[solved] Javascript this/object orientated problems

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Eric Praline
Forum Commoner
Posts: 32
Joined: Wed Aug 29, 2007 8:37 am

[solved] Javascript this/object orientated problems

Post by Eric Praline »

Hi

I have been creating a webpage that has a searchbox with a real-time AJAX autosuggest feature, however have come up against a slight problem...

I've managed to get the search function to use a MySQL database to find names similar to the text entered so far... and return the results as a list like this...

Code: Select all

 
$querySQL = "SELECT DISTINCT Surname FROM `ClientDetails` WHERE Surname LIKE '$nameSearch%'";
$query = mysql_query($querySQL);
 
while ($row = mysql_fetch_array($query)) {
    $LastName .= "<li><a href=\"#\" onClick=\"fillName(this)\">".$row['Surname']."</a></li>";
}
 
The fillName() function looks like this...

Code: Select all

 
function fillName(obj) {
    document.forms["studentCheck"]["LastName"].value = obj.value;
}
 
The details of all the returned search results display into a <div>, and when a name is selected it (should) display it in the searchfield (LastName). However, when I try selecting a name, all I get at the moment is 'undefined', so I must be going wrong somewhere in my Javascript object selection; I've tried various ways of using the 'this' keyword, but can't seem to find the right way of getting the value of the name in the list to then display in the field...

Can anybody give me some pointers please?
Last edited by Eric Praline on Wed Jan 21, 2009 3:50 am, edited 1 time in total.
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: Javascript this/object orientated problems

Post by sergio-pro »

Hi

Passing "this" you pass the "a" element, that was clicked.
And "a" does not have "value" property.

This should work:
document.forms["studentCheck"]["LastName"].value = obj.innerHTML;
Eric Praline
Forum Commoner
Posts: 32
Joined: Wed Aug 29, 2007 8:37 am

Re: [solved] Javascript this/object orientated problems

Post by Eric Praline »

A-ha... cheers, that worked perfectly! Can't get me head round some of this object-orientated Javascript stuff, although I'm sure it'll make sense eventually...

Thanks again! :D
Post Reply