Page 1 of 1

[solved] Javascript this/object orientated problems

Posted: Tue Jan 20, 2009 6:29 am
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?

Re: Javascript this/object orientated problems

Posted: Tue Jan 20, 2009 6:58 am
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;

Re: [solved] Javascript this/object orientated problems

Posted: Wed Jan 21, 2009 3:49 am
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