Jalon Juanis Jalon Juanis - 8 days ago 6
HTML Question

Comparing same div class name value using jquery [SOLVED]

I'm finding way how to comparing value on

<div>
on other div tag that has same class name. So, I'm thingking to do it in jquery. Below is example of
<div>
tag that I want to compare ;

<div class="helloword" value="4"> </div>
<div class="helloword" value="3"> </div>
<div class="helloword" value="2"> </div>
<div class="helloword" value="1"> </div>


I want to make comparasion which which one classes that has smallest value. So it's that possible to do?

Answer

You need to select all the divs by their class, then loop through them and maintain a pointer to the lowest one you've found so far:

var lowestVal = 100; //set this higher than your max value
var divs = $(".helloword"); //select all the elements by their class

//loop through each item and compare it to the lowest recorded one
divs.each(function(index, element) {
  var val = parseInt($(element).attr("value"));
  if (val < lowestVal) lowestVal = val;
});

Now you can use lowestVal for whatever you need.