View Full Version : Javascript / Ajax Suggest Help

08-03-2006, 12:46 PM

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:


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) {

//provide suggestions to the control

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.

08-04-2006, 03: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.