I have a select box on a form, which carries an incremental rel attribute. I have a function that can sort the options by thier .text() value, into alphabetcal order.
<
For future reference, the accepted answer is not complete if option
has data or props (e.g. .prop('selected', true)
). They will be removed after using .html()
.
The function below is probably not optimized but it's correctly working :
$.fn.sortChildren = function(selector, sortFunc) {
$(this)
.children(selector)
.detach()
.sort(sortFunc)
.appendTo($(this));
};
$('select').sortChildren('option', function(a, b) {
// sort impl
});