Created
December 5, 2012 19:48
-
-
Save floydpink/4218887 to your computer and use it in GitHub Desktop.
Sort dropdown/select list items using jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sortDropDownListByText(dropdownlist) { | |
dropdownlist.children("option[value='']").remove(); | |
// Sort all the options by text | |
dropdownlist.html($("option", dropdownlist).sort(function(a, b) { | |
return a.text === b.text ? 0 : a.text < b.text ? -1 : 1; | |
})); | |
dropdownlist.prepend(' '); | |
dropdownlist.children("option[value='']").attr('selected', 'selected'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment