4

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() {
    ??
});
3
  • 5
    According to the HTML Spec, IDs cannot start with a number. Commented Mar 12, 2013 at 20:08
  • 8
    Except in HTML5 and eventually higher. Commented Mar 12, 2013 at 20:09
  • If the elements are arranged in order by ID and only these elements has class note-row then you can do it using .last. -> $('.note-row').last()[0].id Commented Mar 12, 2013 at 20:15

6 Answers 6

17

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);
Sign up to request clarification or add additional context in comments.

10 Comments

+1, much more efficient than the other answer which uses map and sort.
max = Math.max(this.id,max) beats an if-then statement any day.
@Blazemonger Thanks. I was wondering if I needed to add parseInt too, and Math.max should take care of that too. Updated.
Gonna push for an initial value of max = Number.NEGATIVE_INFINITY here as well. :-)
Replaced toArray with get because, well, it's shorter. ;-)
|
8

You could do something like this:

var ids = $('.note-row').map(function() {
    return parseInt(this.id, 10);
}).get();

var max = Math.max.apply(Math, ids);

3 Comments

Except this doesn't actually get the highest for me. The highest in the set I have is 20, whereas this gives me 9.
Other than being inefficient, this will not work as .sort in JS doesn't work with numbers as you'd expect.
@Dogbert: If you're going for efficiency, don't use jQuery in the first place.
2

Funny but this also works:

var max = $('.note-row').sort(function(a, b) { return +a.id < +b.id })[0].id;

http://jsfiddle.net/N5zWe/

2 Comments

Ideally sort comparator functions are supposed to return -1 ot 0 or 1.
@Vega I know, but in this case it's not necessary since we are looking for the max value. Anyway I provided this solution just for the sake of curiosity. Of course it sucks.
2

In the interest of completeness, an optimized Vanilla JS solution:

var n = document.getElementsByClassName('note-row'),
    m = Number.NEGATIVE_INFINITY,
    i = 0,
    j = n.length;
for (;i<j;i++) {
    m = Math.max(n[i].id,m);
}
console.log(m);

1 Comment

This is the fastest technique, if not necessarily the shortest code.
0

The same way you'd find any max, loop:

var max = -999; // some really low sentinel

$('.note-row').each(function() {
    var idAsNumber = parseInt(this.id, 10);
    if (idAsNumber  > max) {
        max = idAsNumber;
    }
});

1 Comment

max = Number.NEGATIVE_INFINITY is the initial value you were looking for.
0
  var maxID = -1;
  $('.note-row').each(function() {
       var myid = parseInt($(this).attr('id'),10);
       if( maxID < myid ) maxID = myid;
  });
  // the value of maxID will be the max from id-s

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.