Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript / Ajax Suggest Help

    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.

    Code:
    /**
     * 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.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •