I have multiple elements with the attribute: data-percentage, is there a way of sorting the elements into ascending order with the lowest value first using either JavaScript or jQuery?
HTML:
<div class="testWrapper">
<div class="test" data-percentage="30">
<div class="test" data-percentage="62">
<div class="test" data-percentage="11">
<div class="test" data-percentage="43">
</div>
Desired result:
<div class="testWrapper">
<div class="test" data-percentage="11">
<div class="test" data-percentage="30">
<div class="test" data-percentage="43">
<div class="test" data-percentage="62">
</div>
Use
Array.sort:Here’s the fiddle: http://jsfiddle.net/UdvDD/
If you’re using IE < 10, you can’t use the
datasetproperty. UsegetAttributeinstead:Here’s the fiddle: http://jsfiddle.net/UdvDD/1/