tags:

views:

158

answers:

2
<div id="container">
    <div>
     <div>
     </div>
    </div>
    <div>
     <div>
     </div>
    </div>
    <div>
     <div>
     </div>
    </div>
</div>

$('#container').find('div') will also include those inner divs.

How to fetch only the 3 top level divs?

+1  A: 

Select only the direct descendants with the parent > child selector:

$('#container > div')

Or the Traversing/children function:

$('#container').children('div')
CMS
What if the id is unknown(container),but only has the jQuery object of $('#container')?A little hard to understand maybe.
Shore
how is `$("#container > div")` harder to understand than `$("#container").children("div")`..? I must say I would prefer the CSS selector any time.
peirix
+1  A: 

You can use jQuery's children([expr]) function to do this.

Something like

$("#container").children("div")
Artem Russakovskii
"Something like .." is most important:)
Shore