Let's say I have a hierarchy of elements, with #root
at its root. I can use $('#root > * > *')
to get all its grandchildren. Is there any way I could do that if I already have $('#root')
?
$('#root').find('* > *')
is definitely not it, as it will happily "start" from any descendant for the first star, not just #root
children.
Is there any function in jQuery that would "anchor" at the current element, starting with its children or itself? It's been bugging me for a while, and I couldn't find anything similar in the docs.
The name attribute selector can be used to select an element by its name. This selector selects elements that have the value exactly equal to the specified value.
I can use $('#root > * > *') to get all its grandchildren.
jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors. All selectors in jQuery start with the dollar sign and parentheses: $().
Either start the find expression with another >
(this syntax is documented):
// This is functionally the same as $('#root > * > *')
$('#root').find('> * > *')
Or call .children()
twice: once for its children and again for its grandchildren:
$('#root').children().children()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With