I'm trying to make searching address form.
This is the structure and methods.
search_address.html :
<form id="search_address_form" onSubmit="sendAddress(); return false;">
<input id="input_address" class="input_address" name="input_address" type="text" placeholder="example: blahblahblah"/>
</form>
<div id="addressList_layer" class="addressList_layer">
<script>
input_address = document.getElementById('input_address');
input_address.addEventListener('keydown',function(){
sendAddress();
});
function sendAddress(){
xhrDocOpen('./php/search_address.php?searchingFor='+inputAddress_data.value,'addressList_layer','get');
return false;
}
</script>
search_address.php :
<?php
include_once('./config.php');
extract($_GET);
echo ($searchingFor);
?>
This code works fine.
But it runs with little bit delayed...
I mean, for example,
input_address.value : "1"
addressList_layer.value : ""
input_address.value : "123"
addressList_layer.value : "12"
input_address.value : "123456"
addressList_layer.value : "12345"
input_address.value : "123abc"
addressList_layer.value : "123ab"
Like this, addressList_layer show always one step delayed than input_address.
I don't know why...
Is there anyone who can guess the reason of this situation and give me the solution?
ps. xhrDocOpen() is...
var xhr = new XMLHttpRequest();
function xhrDocOpen(doc,placeID,method){
xhr.onreadystatechange=function(){
if(xhr.readyState==4 && xhr.status==200){
document.getElementById(placeID).innerHTML=xhr.responseText;
}
xhr.open(method,doc,true);
xhr.send('');
}
This is usual issue if you trying to use "keydown" event.
Just try to change it to "keyup" and it should work.