Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference direct descendent (>) vs. descendant in jQuery selectors?

What's the difference between these two jQuery statements? They seem to do the same thing by getting all the children div tags.

$("#mainblock div")
$("#mainblock > div")
like image 631
Tom Avatar asked Apr 19 '12 07:04

Tom


People also ask

What is the difference between descendant and child selectors?

Descendant Selector :The descendant Selector selects all child elements of the selected parent element. In this example, the descendant selector selects all li of selected ul elements. 2. Direct Child Selector : This selector selects only the direct child of the selected element.

What is a descendant selector?

The descendant selector is a way to select elements that are located somewhere underneath other elements, according to the tree structure of the webpage. This selector is actually multiple selectors combined together, but separated by a space.

What is jQuery descendant?

With jQuery you can traverse down the DOM tree to find descendants of an element. A descendant is a child, grandchild, great-grandchild, and so on.


2 Answers

$("#mainblock > div") = the childs only level

$("#mainblock div") = all the childs+ desendents.

like image 112
Royi Namir Avatar answered Oct 01 '22 21:10

Royi Namir


Have a look at jQuery Selectors

Child Selector ("parent > child") - Hierarchy Selects all direct child elements specified by "child" of elements specified by "parent".

Descendant Selector ("ancestor descendant")- Hierarchy Selects all elements that are descendants of a given ancestor.

like image 23
Pranay Rana Avatar answered Oct 01 '22 21:10

Pranay Rana