Posted By

tylerhall on 11/30/-1


Tagged

menu dropdown sort list foo


Versions (?)

Who likes this?

9 people have marked this snippet as a favorite

jonhenshaw
bomberstudios
luman
talaris
visualAesthetic
happolati
vali29
goeneas
seanpowell


Sort Dropdown Menu


 / Published in: JavaScript
 

Takes a select box ID and sorts the values - maintains key index pairs.

  1. function sortList(id) {
  2. var obj = document.getElementById("id");
  3. var values = new Array();
  4. for(var i = 0; i < obj.options.length; i++) {
  5. values.push(obj.options[i].innerHTML + "--xx--" + obj.options[i].value);
  6. }
  7.  
  8. values = values.sort();
  9.  
  10. for(var i = 0; i < values.length; i++) {
  11. valueArray = values[i].split('--xx--');
  12. obj.options[i].innerHTML = valueArray[0];
  13. obj.options[i].value = valueArray[1];
  14. }
  15. }

Report this snippet  

You need to login to post a comment.