Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

max value of data attribute using jquery

Tags:

jquery

i have multiple division with different data attribute value, with same class also need to get the value data-value using jquery. Example i have data-value groups 2, 3, 5 to get the desired result is 5 among the group

<div data-value="2" class="maindiv">test</div>
<div data-value="5" class="maindiv">test</div>
<div data-value="3" class="maindiv">test</div>
etc.
like image 839
Muhammad ali Avatar asked Mar 18 '17 12:03

Muhammad ali


2 Answers

There is no direct way but this will do

var dataList = $(".maindiv").map(function() {
    return parseInt($(this).attr("data-value"));
}).get();
console.log(Math.max.apply(null, dataList));

https://jsfiddle.net/pgbf3o9f/

like image 155
Suresh Atta Avatar answered Oct 11 '22 02:10

Suresh Atta


You can try the $.each() method, like below:

var result = 0;

$('.maindiv').each(function(index) {
    if ($(this).data('value') > result) {
        result = $(this).data('value');
    }
});
// result now contains the max value, so do what you want with it
console.log(result);
like image 25
Michael Evans Avatar answered Oct 11 '22 03:10

Michael Evans