omg
omg

Reputation: 139982

How to fetch only top-most level div with jQuery?

<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?

Upvotes: 1

Views: 1693

Answers (2)

Artem Russakovskii
Artem Russakovskii

Reputation: 22023

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

Something like

$("#container").children("div")

Upvotes: 2

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827704

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

$('#container > div')

Or the Traversing/children function:

$('#container').children('div')

Upvotes: 1

Related Questions