So, I'm making a custom javascript autocomplete search box using ajax and I'm kinda stuck. I'm using the results of the ajax response to populate an ul and that's all working great. The problem is when I try to change the class of the li element on mouseover. I'm passing the this keyword to the function call but it's actually referencing the xmlHttp object not the li. So my question is how do I reference the li in this case? I've searched for the solution but came up with nothing and since I'm still new to javascript thought I ask my first question and get help from you fine people. Here is the code:
request = new ajaxRequest()
// Calling the ajax_DB.php script
request.open("POST", "ajax_DB.php", true)
// Setting the headers
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
request.setRequestHeader("Content-length", input.length)
request.setRequestHeader("Connection", "close")
request.send(input)
// On successfull server response display the data
request.onreadystatechange = function(event)
{
if (this.readyState == 4)
{
if (this.status == 200)
{
if (this.responseText != null)
{
// Display results if they are hidden
display.style.display = 'block';
// If there are results
if (display.firstChild !== undefined)
{
// Remove them
while(display.firstChild)
{
display.removeChild(display.firstChild)
}
}
// Convert the string result to an array
var results = this.responseText.split(',');
// Remove last element from the array(',')
results.pop();
// Append one 'li' element for each array element
for (var i = 0; i < results.length; i++)
{
var li = document.createElement('li')
li.setAttribute("onclick", "setInputVal(this.innerHTML)")
li.setAttribute("onmouseover", setClassOnMouseOver(this))
li.innerHTML = results[i]
display.appendChild(li)
}
}
else alert("Ajax error: No data received")
}
else alert("Ajax error: " + this.statusText)
}
}