views:

29

answers:

5

hi. i've a list of div linked to a main div-root. How can browse all elements of the main div? example :

<div id="main">
    <div class="trackon" id="trackline1">Line 1</div>
    <div class="trackon" id="trackline2">Line 2</div>
    <div class="trackon" id="trackline3">Line 3</div>
    <div class="trackoff" id="trackline4">Line 4</div>
    <div class="trackoff" id="trackline5">Line 5</div>
</div>

i'm looking for a jquery function that browse all elements (like a for each statament) of #main div. I've tried to search on official documentation but i find nothing! cheers

+3  A: 
$('#main div').each(function(){
//$(this) is the current div
});

Or, if you want to make sure you just grab first level divs,

$('#main > div').each(function(){
//$(this) is the current div
});
Stefan Kendall
yeah! in fact i miss to specific a thing : and if i want to select only children div which have a specific class? in that example all child with "trackon" class. I use .find() ?
markzzz
To get elements of just one class, you can use: `$('#main>div.trackon')`. This will get all divs (with a class of 'trackon') that are children of main.
Rocket
great!! tnx man :)
markzzz
+1  A: 
$('#main>div').each(function(){
    alert($(this).attr('id'));
});
Rocket
A: 

You can use the each like this:

$('$main > div').each(function(){
  // your code...
});

The > is child selector which is used here to select direct/immediate children of parent div.

Sarfraz
A: 
$('#main').children().each(function(){
    alert($(this));
});
Fred Bergman
A: 

another (related) question : can i put somethings like this?

$('.tlcontent div>div.trackon>div.trackcounter').each(function(){
    $(this).html(idtl++);
});

tlcontent, trackon and trackcounter are each a class selector.

it looks like an error :)

markzzz