...

View Full Version : Javascript / Ajax Suggest Help



tehfincheh
08-03-2006, 01:46 PM
Hey,

I've run across a slight problem trying to reproduce the popular Suggest script where as you type in to a textbox, the options appear in a dropdown box.

I'm new to Javascript and as the script is, the autocomplete feature works - but it only offers one possible record, and the record appears in the textbox instead of as an option in a dropdown list.

For a better example:

http://www.imajinn.co.uk/phonebook

If you type "sh" in the phonebook search box, you'll see "sharon harding" appear automatically. But what i need is for the options to be displayed in dropdown form, and for it to include all possible options.

I didn't code the original file personally, but i presume this is the .js that would need to be modified.



/**
* Provides suggestions for state names (USA).
* @class
* @scope public
*/
function StateSuggestions() {
this.states = [
"Amanda Lacey", "darryn cook", "Kevin Bradley", "Sharon Harding",
"sharon harding", "Darryn Cook", "amanda lacey",
"caroline abbondanza", "Caroline Abbondanza" , "casey smith"
];
}

/**
* Request suggestions for the given autosuggest control.
* @scope protected
* @param oAutoSuggestControl The autosuggest control to provide suggestions for.
*/
StateSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/) {
var aSuggestions = [];
var sTextboxValue = oAutoSuggestControl.textbox.value;

if (sTextboxValue.length > 0){

//search for matching states
for (var i=0; i < this.states.length; i++) {
if (this.states[i].indexOf(sTextboxValue) == 0) {
aSuggestions.push(this.states[i]);
}
}
}

//provide suggestions to the control
oAutoSuggestControl.autosuggest(aSuggestions);
};

Sorry for being so vague, but if anybody could point me in the right direction for where to start here, it'd be much appreciated. Thanks.

A1ien51
08-04-2006, 04:18 PM
Well that script is not set up to do that.

I would say look at this: http://developer.yahoo.com/yui/autocomplete/

in there download they have a sample that uses a JS array to do exactly what you are asking.

Eric



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum