Revision: 29438
Updated Code
at July 29, 2010 14:23 by Meander365
Updated Code
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); };
Revision: 29437
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at July 29, 2010 14:21 by Meander365
Initial Code
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); };
Initial URL
http://rickyrosario.com/blog/sorting-dropdown-select-options-using-jquery/
Initial Description
Incredibly useful. Great when you need to remove and add options to selectboxes.
Initial Title
Sort Options in a Select Box
Initial Tags
dropdown, sort, jquery
Initial Language
jQuery