Last active
July 13, 2021 13:12
-
-
Save amelieykw/c23e7e730e7bab9c75d92bcacbad2d26 to your computer and use it in GitHub Desktop.
[jQuery - sort divs - by 'data-' attribute] #jQuery #javascript #sort_divs
This file contains hidden or 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
div[data-percentage] { | |
font-family: sans-serif; | |
background: teal; | |
padding: 10px; | |
font-weight: bold; | |
color: #023636; | |
text-shadow: 0 1px 0 #58BBBB; | |
font-size: 20px; | |
width: 100px; | |
margin: 4px auto; | |
text-align: center; | |
} | |
div[data-percentage]:before { | |
content: attr(data-percentage); | |
} |
This file contains hidden or 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
<div class="testWrapper"> | |
<div class="test" data-percentage="30"></div> | |
<div class="test" data-percentage="62"></div> | |
<div class="test" data-percentage="11"></div> | |
<div class="test" data-percentage="43"></div> | |
</div> |
This file contains hidden or 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
var $wrapper = $('.testWrapper'); | |
$wrapper.find('.test').sort(function (a, b) { | |
return +a.dataset.percentage - +b.dataset.percentage; | |
}) | |
.appendTo( $wrapper ); |
This file contains hidden or 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
<div id="list"> | |
<div class="listing-item" data-listing-price="2">2</div> | |
<div class="listing-item" data-listing-price="3">3</div> | |
<div class="listing-item" data-listing-price="1">1</div> | |
<div class="listing-item" data-listing-price="4">4</div> | |
</div> |
This file contains hidden or 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
var divList = $(".listing-item"); | |
divList.sort(function(a, b){ return $(a).data("listing-price")-$(b).data("listing-price")}); | |
$("#list").html(divList); |
This file contains hidden or 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
li:before {content: attr(data-time) ": "; color: #aaa;} |
This file contains hidden or 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
<ul> | |
<li data-time="5">One</li> | |
<li data-time="523">Two</li> | |
<li data-time="12">Three</li> | |
<li data-time="3">Four</li> | |
<li data-time="2">Five</li> | |
<li data-time="12">Six</li> | |
<li data-time="1">Seven</li> | |
<li data-time="678">Eight</li> | |
</ul> |
This file contains hidden or 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
var items = $('li'); | |
items.sort(function(a, b){ | |
return +$(a).data('time') - +$(b).data('time'); | |
}); | |
items.appendTo('ul'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment