Answers for ""how to sort option elements alphabetically using jquery""

2

jquery sort select options by text

function alphabetizeList() {
    var sel = $(listField);
    var selected = sel.val(); // cache selected value, before reordering
    var opts_list = sel.find('option');
    opts_list.sort(function (a, b) {
        return $(a).text() > $(b).text() ? 1 : -1;
    });
    sel.html('').append(opts_list);
    sel.val(selected); // set cached selected value
}

alphabetizeList('#FIELDID');
Posted by: Guest on February-27-2020
1

sort divs alphabetically jquery

$('.sortme').sort(function(a, b) {
  if (a.textContent < b.textContent) {
    return -1;
  } else {
    return 1;
  }
}).appendTo('body');
Posted by: Guest on August-30-2021

Code answers related to ""how to sort option elements alphabetically using jquery""

Code answers related to "Javascript"

Browse Popular Code Answers by Language