I have been using the following AJAX search script for a very long time and it works fine unless the Enter key is pressed… which results in clearing the search parameters.
According to the following link all I have to do is to insert a single JavaScript line to disable enter:
Mozilla.org->preventDefault();
I have tried to insert event.preventDefault();" in numerous places and all it does is to prevent the search from working
script>
var liveSearch = function(inputID) {
this.input = document.getElementById(inputID);
if (this.input) {
this.initialise();
}
}
liveSearch.prototype = {
initialise: function()
{
this.previousSearch = null;
this.canSearch = 1;
this.input.addEventListener("keyup", this.onKeyUp.bind(this));
},
onKeyUp : function()
{
if (this.canSearch) {
this.canSearch = 0;
setTimeout(function(){
var value = this.input.value.toLowerCase();
if (value != this.previousSearch) {
this.doSearch(value);
}
this.previousSearch = value;
this.canSearch = 1;
}.bind(this), 500);
}
},
doSearch : function(str)
{
// START: do your ajax call here ============================================
if (str.length==0)
{
document.getElementById("livesearch").innerHTML="";
// document.getElementById("livesearch").style.border="0px";
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("livesearch").innerHTML=xmlhttp.responseText;
// document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
xmlhttp.open("GET", "AJAX-0048.php?q="+str,true);
xmlhttp.send();
// END: do your ajax call here ============================================
}
}
var myLiveSearch = new liveSearch("bufferedInput");
</script>