javascript - How to sort elements in a list with numbers? -
this question has answer here:
i trying sort list:
<div id="mainmenu"> <span>thing 5</span> <span>thing 4</span> <span>thing 7</span> <span>thing 1</span> <span>thing 5</span> <span>thing 6</span> <span>thing 3</span> </div>
i have tried:
$('#mainmenu').sort(function(){ $(this).children().sort(); });
i want sorted in order:
<span>thing 1</span> <span>thing 2</span> <span>thing 3</span> <span>thing 4</span> <span>thing 5</span> <span>thing 6</span> <span>thing 7</span>
try this,
$span = $('#mainmenu span').sort(function(a, b) { if ($(a).text() < $(b).text()) return -1; if ($(a).text() > $(b).text()) return 1; return 0; }); $('#mainmenu').html($span);
Comments
Post a Comment