RESOLVED: Ajax firstimer: onchange not working right
Posted: Mon Nov 15, 2010 10:47 am
Hi
I am new to Ajax but loving it.
Trying to use it for a search tool, so that when you start typing in your keywords, it begins to display answers from the DB.
I have done it, but only after typing once you either click away, or clickaway - and then back on it again.
I assumed that 'onchange' means when it changes it does a search based on what is has changed too. It does, but not the instant you are typing. Is there a slightly different terminology I should be using rather than 'onchange' to achieve this?
NO SOONER DO I FIND THE PROBLEM, THAN I FIND A LISTING OF ALL EVENT HANDLERS AND THE ANSWER WAS:
onKeyDown
Many thanks if you read this.
I am new to Ajax but loving it.
Trying to use it for a search tool, so that when you start typing in your keywords, it begins to display answers from the DB.
I have done it, but only after typing once you either click away, or clickaway - and then back on it again.
Code: Select all
<form method='post' action='index.php?page=search&menu=home' name="searchbox" onSubmit="return formCheck(this)">
<input type='text' name='search' class='searchbar' value='' onchange="precheck(this.value)"/>
<input type='submit' value='Search' class='submitsearch' />
</form>Code: Select all
<script>
function precheck(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("srcHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","presearch.php?q="+str,true);
xmlhttp.send();
}
</script>onKeyDown
Many thanks if you read this.