How To Find Maximum Value Of Attribute in Jquery?

May 11, 2024 | HTML


Hello Dev,

In this comprehensive tutorial, we'll delve into how to use jQuery to retrieve the maximum attribute value. We'll discuss how to obtain custom attribute values by class using jQuery and explore step-by-step how to find the maximum value of an attribute using jQuery.

At times, you may encounter multiple tags with the same class and attribute values, and the task may involve finding the maximum value of that common attribute. Consider the scenario where you have identical HTML tags:

Now, the objective is to retrieve the maximum value, such as 5, as it's the largest value among others. So, you can obtain the maximal value using the following approach:

Read Also: Scrolling Table with Fixed Header Html Example Tutorial index.php
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How To Find Maximum Value Of Attribute in Jquery? -- ItErrorSolution.com</title>
</head>
<body>
  <h1>How To Find Maximum Value Of Attribute in Jquery? -- ItErrorSolution.com</h1>
<ul>
    <li class="test" data-id="3">hi</li>
    <li class="test" data-id="5">hey</li>
    <li class="test" data-id="1">hello</li>
</ul>
  
<script type="text/javascript">
      
    var max = 0;
    $('.test').each(function() {
      var value = parseInt($(this).data('id'));
      max = (value > max) ? value : max;
    });
    alert(max);
  
</script>
  
</body>
</html>

Thank you for your encouragement! If you have any questions or need further assistance, feel free to ask. I'm here to help!



Tags :
#HTML
ItErrorSolution.com

ItErrorSolution.com

"Hey there! I'm a full-stack developer and proud owner of ItErrorSolution.com, based right here in India. I love nothing more than sharing handy tips and tricks with my fellow developers through easy-to-follow tutorials. When it comes to coding, I'm all about PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, CodeIgniter, and Bootstrap – been hooked on them forever! I'm all about putting in the work and staying committed. Ready to join me on this journey to coding?"