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?
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: HTML:
<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>
using either Javascript or jQuery?
A more robust option, this function can allow you to sort elements based on multiple options.
https://jsfiddle.net/L3rv3y7a/1/
I think that the Tinysort Jquery plugin should be an option, you can get it i here: http://tinysort.sjeiti.com/
I did not tried it but the code should look like this:
hope this will help
Use
Array.sort
:Here's the fiddle: http://jsfiddle.net/UdvDD/
If you're using IE < 10, you can't use the
dataset
property. UsegetAttribute
instead:Here's the fiddle: http://jsfiddle.net/UdvDD/1/
For some reason, on Firefox 64.0.2, none of the answers worked for me. This is what worked in the end, a mixture of Joseph Silber and Jeaf Gilbert's answers: