tags:

views:

43

answers:

3

Newbie jQuery question:

How do I find out what number a given div is in the DOM-tree?

$('div.dashboard-module').each( function() {
    var divNumber = $(this).[DON'T KNOW WHAT TO WRITE HERE];
});

alert('This div is number ' + divNumber );

Hope it makes sense! :)

+1  A: 

If you want to know the index of the element you are currently iterating you can use the first argument of the callback function:

$('div.dashboard-module').each(function (index) {
    var divNumber = index;
});

More info:

CMS
Thanks a bunch! :D
timkl
+1  A: 

What are you looking for the number relative to?

If its the document, then you need

$('div.dashboard-module').each(function(){
    var divNumber = $(document).index($(this));
});

If its relative to the parent, then you need

$('div.dashboard-module').each(function(){
    var divNumber = $($(this).parent()).index($(this));
});

etc.

Comment if you've got any questions

Gausie
A: 
var divNumber = $('div.dashboard-module');
alert("This div is number " + (divNumber.length-1) + ". " + $(divNumber[divNumber.length-1]).html() );
andres descalzo