Home >Web Front-end >JS Tutorial >JavaScript method to sort drop-down list values_javascript skills

JavaScript method to sort drop-down list values_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:50:141708browse

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn