dimanche 11 janvier 2015

loop through a html select list except for the last value


Vote count:

0




I am learning Javascript / JQuery and I have cobbled together from a tutorial some code to alphabetically sort a HTML list from A - Z.


The code I have seems to be working OK.


But now I must alphabetically sort through the entire list from A-Z, except the last value.


I cannot figure out how to sort all but the last value. I searched SO and Google, but still cannot figure it out.


Here is my code:



function sortCurrentSectorTypes() {

// sort the sector types list in alphabetical order (A-Z) after language code has changed.

var selected = $("#id_current_sector_type").val(); // preserve the users selected value.
var options = $('#id_current_sector_type option');
var arr = options.map(function(_, o) {

return {t: $(o).text(), v: o.value};

}).get();

arr.sort(function(o1, o2) {

return o1.t.toLowerCase() > o2.t.toLowerCase() ? 1 : o1.t.toLowerCase() < o2.t.toLowerCase() ? -1 : 0;

});

options.each(function(i, o) {

console.log(i);

o.value = arr[i].v;

$(o).text(arr[i].t);

});

$("#id_current_sector_type").val(selected); // assign the preserved users selected value.

}


asked 1 min ago







loop through a html select list except for the last value

Aucun commentaire:

Enregistrer un commentaire