I am an absolute newb to Javascript so my apologies. I have got an autocomplete script adapted and working, but when I click elsewhere on the screen or tab away from the input field, the results list doesn't go away. I have been trying different approaches for hours with no luck.
function findCustomer(thevalue, e) {
if (thevalue.length > 1) {
theObject = document.getElementById("autocompletediv");
theObject.style.visibility = "visible";
theObject.style.width = "152px";
var posx = 0;
var posy = 0;
posx = (findPosX (document.getElementById("surname")) +200);
posy = (findPosY (document.getElementById("surname")) +0);
theObject.style.left = posx + "px";
theObject.style.top = posy + "px";
var theextrachar = e.which;
if (theextrachar == undefined){
theextrachar = e.keyCode;
}
var objID = "autocompletediv";
if (thevalue.length == 1) {
var serverPage = "findcustomer.php";
} else {
var serverPage = "findcustomer.php" + "?sn=" + thevalue.substr (0, (thevalue.length));
}
var obj = document.getElementById(objID);
var request = new XMLHttpRequest();
request.open("GET", serverPage);
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
obj.innerHTML = request.responseText;
}
}
request.send(null);
} else {
theObject.style.visibility = "hidden";
}
}
This works, shows the div when 2 characters or more entered, hides when 1 or no characters entered.
I thought an onBlur should work so I have tried the following:
<td><input name="surname" type="text" class="short" id="surname" autocomplete="off" onKeyUp="findCustomer(this.value, event)" onBlur="hideDiv()" /></td>
And the function:
function hideDiv() {
document.getElementById('autocompletediv').style.visibility = "hidden";
}
I'm sure it's blatently obvious where I'm going wrong to most of you...