Skip to content

Instantly share code, notes, and snippets.

@jomasero
Created June 24, 2013 20:24
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jomasero/5853295 to your computer and use it in GitHub Desktop.
Save jomasero/5853295 to your computer and use it in GitHub Desktop.
Ordenar alfabéticamente una lista dentro de un select que viene desordenada.
<!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