/ Published in: jQuery
Incredibly useful. Great when you need to remove and add options to selectboxes.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
jQuery.fn.sort = function() { return this.pushStack([].sort.apply(this, arguments), []); }; jQuery.fn.sortOptions = function(sortCallback) { jQuery('option', this) .sort(sortCallback) .appendTo(this); return this; }; jQuery.fn.sortOptionsByText = function() { var byTextSortCallback = function(x, y) { var xText = jQuery(x).text().toUpperCase(); var yText = jQuery(y).text().toUpperCase(); return (xText < yText) ? -1 : (xText > yText) ? 1 : 0; }; return this.sortOptions(byTextSortCallback); }; jQuery.fn.sortOptionsByValue = function() { var byValueSortCallback = function(x, y) { var xVal = jQuery(x).val(); var yVal = jQuery(y).val(); return (xVal < yVal) ? -1 : (xVal > yVal) ? 1 : 0; }; return this.sortOptions(byValueSortCallback); };
URL: http://rickyrosario.com/blog/sorting-dropdown-select-options-using-jquery/