Results 1 to 2 of 2

Thread: Add item to beginning / top of dropdown list

  1. #1

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Add item to beginning / top of dropdown list

    How to one ensure the following will add an option to a dropdownlist, but at the top of the list? (since the list will already be populated in my case)
    Code:
        $.fn.prependSelect = function (value, text, selected) {
            return this.each(function () {
                if (this.tagName == 'SELECT') {
                    var dropdownList = this;
                    var option = new Option(text, value, selected);
                    if ($.browser.msie) {
                        dropdownList.add(option);
                    } else {
                        dropdownList.add(option, null);
                    }
                }
            });
        };
    Thanks in advance!


  2. #2
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: Add item to beginning / top of dropdown list

    Since you're already using jQuery, just go the whole hog and use the jQuery DOM methods to do this. Any performance hit has already been taken by loading jQuery in the first place.

    You would want either prepend() or prependTo(). $.browser is deprecated, so you shouldn't be using it; and in this instance you don't actually need it. jQuery methods should handle all the cross-browser bugs internally.

    Example:
    http://jsfiddle.net/tr333/Pk78H/1/
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

Posting Permissions

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



Click Here to Expand Forum to Full Width