jquery - Javascript select element that has class with max value -
i know there many topics jquery selector regex, need this
i have div :
<div class="some random things rank0">hello</div> <div class="some things rank1">hello</div> <div class="things rank1">hello</div> <div class="some random rank2 things">hello</div> <div class="random rank2 some">hello</div> <div class="some things rank3">hello</div> <div class="some random rank4">hello</div>
i selector select every div rank > specific_value
can't modify html. know should use filter
function, i'm not in jquery/regex. there start :
var specific_value = 2; $.each($("div").filter(function() { // need // return rank > specific_value }), function() { $(this).html("test"); })
expected result :
<div class="some random things rank0">hello</div> <div class="some things rank1">hello</div> <div class="things rank1">hello</div> <div class="some random rank2 things">hello</div> <div class="random rank2 some">hello</div> <div class="some things rank3">test</div> <div class="some random rank4">test</div>
i have used simple jquery/javascript code filter out result. not in regex:
var specific_value = 2; $.each($("div").filter(function() { var classes = $(this).attr("class").split(" "); var matchfound = false; (i = 0; < classes.length; i++) { if (classes[i].indexof("rank") > -1) { var rank = classes[i]; rank = rank.replace("rank", ""); rank = parseint(rank, 10); if(rank > specific_value) matchfound = true; } } return matchfound; }), function() { $(this).html("test"); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="some random things rank0">hello</div> <div class="some things rank1">hello</div> <div class="things rank1">hello</div> <div class="some random rank2 things">hello</div> <div class="random rank2 some">hello</div> <div class="some things rank3">hello</div> <div class="some random rank4">hello</div>
Comments
Post a Comment