Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery select elements on 1st "level"

$("#BaseElement").children("span");

or

$("#BaseElement > span");

See jQuery selectors and children().


In the jQuery API, when it refers to 'descendants' it means all levels, and when it refers to 'children' it means only the first level

this will get you all first level children (in your example the first p, div, 2 spans, and last p)

$('#BaseElement > *')

Also refer to What is the difference direct descendent (>) vs. descendant in jQuery selectors? for differences between space and > in selectors. With space equals to find() and > equals to children(). In other words, the following pairs are equivalent,

$("#BaseElement").children("span");
$("#BaseElement > span");

Get all children plus their descendants:

$("#BaseElement").find("span");
$("#BaseElement span");