Jalon Juanis Jalon Juanis - 1 year ago 77
HTML Question

Comparing same div class name value using jquery

I'm finding way how to comparing value on

on other div tag that has same class name. So, I'm thingking to do it in jquery. Below is example of
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download