Created
June 24, 2013 20:24
-
-
Save jomasero/5853295 to your computer and use it in GitHub Desktop.
Ordenar alfabéticamente una lista dentro de un select que viene desordenada.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> | |
<script> | |
function ordenarSelect(id_componente) | |
{ | |
var selectToSort = jQuery('#' + id_componente); | |
var optionActual = selectToSort.val(); | |
selectToSort.html(selectToSort.children('option').sort(function (a, b) { | |
return a.text === b.text ? 0 : a.text < b.text ? -1 : 1; | |
})).val(optionActual); | |
} | |
$(document).ready(function () { | |
ordenarSelect('selectPaises'); | |
}); | |
</script> | |
</head> | |
<body> | |
<select id="selectPaises"> | |
<option>Costa Rica</option> | |
<option>Mezenao</option> | |
<option>Argentina</option> | |
<option>México</option> | |
<option>Marruecos</option> | |
</select> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment