How to Get Max Attribute Value in JQuery?

By Hardik Savani January 31, 2023 Category : Javascript HTML jQuery

In this profound tutorial, we will learn jquery get max attribute value. let’s discuss about jquery get custom attribute value by class. I would like to show you jquery find max attribute value. step by step explain find maximum value of attribute jquery.

Sometimes, you have a many tag with same class and attribute with value, but you need to find maximal value of attribute from that conman class, In bellow you have same HTML tag:

Now, you require to get max value, i mean you need to 5, because that is big value from other, so you can get maximal value from this way:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title></title>

</head>

<body>

<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>

I hope it can help you...

Tags :
Shares