Skip to content

Instantly share code, notes, and snippets.

@floydpink
Created December 5, 2012 19:48
Show Gist options
  • Save floydpink/4218887 to your computer and use it in GitHub Desktop.
Save floydpink/4218887 to your computer and use it in GitHub Desktop.
Sort dropdown/select list items using jQuery
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