RESOLVED: Ajax firstimer: onchange not working right

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

RESOLVED: Ajax firstimer: onchange not working right

Post by simonmlewis »

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.

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>
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?

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>
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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply