Created
July 27, 2013 11:31
-
-
Save boy3vil/6094611 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript" src="../js/jquery.min.js"></script> | |
<script type="text/javascript"> | |
$(document).ready(function(){ | |
$('button').click(function() { | |
var options = $('select.whatever option'); | |
var arr = options.map(function(_, o) { | |
return { | |
t: $(o).text(), | |
v: o.value | |
}; | |
}).get(); | |
arr.sort(function(o1, o2) { | |
return o1.t > o2.t ? 1 : o1.t < o2.t ? -1 : 0; | |
}); | |
options.each(function(i, o) { | |
console.log(i); | |
o.value = arr[i].v; | |
$(o).text(arr[i].t); | |
}); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<button type=button>Sort Options</button> | |
<select class='whatever'> | |
<option value='22'>Hello</option> | |
<option value='101'>Banana</option> | |
<option value='1'>Sugar Cane</option> | |
<option value='-2'>Palm Oil</option> | |
</select> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment