php&javascript - how to highlight the lowest value -
my goal , highlight lowest value between decimals within li.
<ul class="list-group"> <li class="list-group-item" style="border:2px solid ;" data-distance="31.5">test 31.5</li> <li class="list-group-item" style="border:2px solid ;" data-distance="21.5">test 21.5</li> <li class="list-group-item" style="border:2px solid ;" data-distance="11.5">test 11.5</li> <li class="list-group-item" style="border:2px solid ;" data-distance="51.5">test 51.5</li> <li class="list-group-item" style="border:2px solid ;" data-distance="61.5">test 61.5</li> <li class="list-group-item" style="border:2px solid ;" data-distance="11.5">test 11.5</li> </ul>
my code:
<script type='text/javascript'> $(window).load(function() { $($("[data-distance]").get().sort(function(a, b) { return a.getattribute("data-distance") - b.getattribute("data-distance"); })[0]).css("border-color", "red"); }); </script>
this code fails if multiple li has same min value. want highlight each min value— how can this?
try this:
// lowest value first var low = $("[data-distance]").get().sort(function(a, b) { return a.getattribute("data-distance") - b.getattribute("data-distance"); })[0].getattribute("data-distance"); // loop through , update match lowest $("[data-distance]").each(function(a){ if (this.getattribute("data-distance") == low) { $(this).css("border-color", "red"); } });
Comments
Post a Comment