I have a bunch of note divs in the following format:
<div class="note-row" id="1">
<div class="note-row" id="2">
<div class="note-row" id="4">
<div class="note-row" id="5">
<div class="note-row" id="6">
How would I get the largest id using javascript? So far I have:
$('.note-row').each(function() {
    ??
});
Quick and dirty way:
var max = 0;
$('.note-row').each(function() {
    max = Math.max(this.id, max);
});
console.log(max); 
This is a little shorter and more sophisticated (for using reduce, and also allowing negative ids down to Number.NEGATIVE_INFINITY, as suggested by Blazemonger):
var max = $('.note-row').get().reduce(function(a, b){
    return Math.max(a, b.id)
}, Number.NEGATIVE_INFINITY);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With