Home > Article > Web Front-end > JavaScript method to sort drop-down list values_javascript skills
The example in this article describes how to use JavaScript to sort drop-down list values. Share it with everyone for your reference. The details are as follows:
function sortList(id) { var obj = document.getElementById("id"); var values = new Array(); for(var i = 0; i < obj.options.length; i++) { values.push(obj.options[i].innerHTML + "--xx--" + obj.options[i].value); } values = values.sort(); for(var i = 0; i < values.length; i++) { valueArray = values[i].split('--xx--'); obj.options[i].innerHTML = valueArray[0]; obj.options[i].value = valueArray[1]; } }
I hope this article will be helpful to everyone’s JavaScript programming design.