Jump to content

Autosuggest/Livesearch alterations


klewis

Recommended Posts

HiI am using an autosuggest script on my site written by dynamicAJAX, but I want to adapt it slightly and I am clueless when it comes to the javascript side.What I am trying to do is set a 2 keystroke delay before the autosuggest comes up, and if also possible an 'arrow key scroll function. I think you have to put .minQueryLength = 2; somewhere but I have tried it in several different places to no avail. This is the code I have:

//Gets the browser specific XmlHttpRequest Objectfunction getXmlHttpRequestObject() {	if (window.XMLHttpRequest) {		return new XMLHttpRequest();	} else if(window.ActiveXObject) {		return new ActiveXObject("Microsoft.XMLHTTP");	} else {		alert("Your Browser is VERY old!\nIt's about time to upgrade don't you think?");	} }//Our XmlHttpRequest object to get the auto suggestvar searchReq = getXmlHttpRequestObject();//Called from keyup on the search textbox.//Starts the AJAX request.function searchSuggest() {	if (searchReq.readyState == 4 || searchReq.readyState == 0) {		var str = escape(document.getElementById('txtSearch').value);		searchReq.open("GET", 'searchSuggest.php?search=' + str, true);		searchReq.onreadystatechange = handleSearchSuggest; 		searchReq.send(null);	}	}//Called when the AJAX response is returned.function handleSearchSuggest() {	if (searchReq.readyState == 4) {		var ss = document.getElementById('search_suggest')		ss.innerHTML = '';		var str = searchReq.responseText.split("\n");		for(i=0; i < str.length - 1; i++) {			//Build our element string.  This is cleaner using the DOM, but			//IE doesn't support dynamically added attributes.			var suggest = '<div onmouseover="java script:suggestOver(this);" ';			suggest += 'onmouseout="java script:suggestOut(this);" ';			suggest += 'onclick="java script:setSearch(this.innerHTML);" ';			suggest += 'class="suggest_link">' + str[i] + '</div>';			ss.innerHTML += suggest;		} } }//Mouse over functionfunction suggestOver(div_value) {	div_value.className = 'suggest_link_over';}//Mouse out functionfunction suggestOut(div_value) {	div_value.className = 'suggest_link';}//Click functionfunction setSearch(value) {	document.getElementById('txtSearch').value = value;	document.getElementById('search_suggest').innerHTML = '';}

Any help will be gratefully appreciated and accredited, and I promise that learning some javascript is on my list!Many Thanks in advanceKelly

Link to comment
Share on other sites

I don't know about the minQueryLength property bit, I can't see any objects that would use it in that code, unless there is a bit missing.I would just check to see whether the search string was longer than 2 chars

function searchSuggest() {	if (document.getElementById('txtSearch').value.length >= 2) {		if (searchReq.readyState == 4 || searchReq.readyState == 0) {			var str = escape(document.getElementById('txtSearch').value);			searchReq.open("GET", 'searchSuggest.php?search=' + str, true);			searchReq.onreadystatechange = handleSearchSuggest;			searchReq.send(null);		}	}}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...