Last active
December 20, 2015 02:19
-
-
Save oriolrivera/6055235 to your computer and use it in GitHub Desktop.
Ordenar alfabéticamente elementos del html con jquery
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 lang="es"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Ordenar alfabéticamente elementos del html con jquery</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> | |
<script> | |
$(function(){ | |
var mylist = $('#myId'); | |
var listitems = mylist.children('li').get(); | |
listitems.sort(function(a, b) { | |
var compA = $(a).text().toUpperCase(); | |
var compB = $(b).text().toUpperCase(); | |
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0; | |
}) | |
$.each(listitems, function(idx, itm) { mylist.append(itm); }); | |
}); | |
</script> | |
</head> | |
<h1>ordenar alfabéticamente elementos del html con jquery</h1> | |
<ul id="myId"> | |
<li>b</li> | |
<li>a</li> | |
<li>c</li> | |
<li>m</li> | |
<li>a</li> | |
</ul> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment