How to get the children of the $(this) selector?
Directly grab an element's descendants using jQuery's .children()
function in tandem with $(this)
:
If you're interested to see all lower generations, not just the immediate offsprings, include .find()
:
For more structured java-nese, utilize context selector "img", this
for a clean call:
Extending your toolkit
Select by class or tag name
When you need to filter child selection by their tagName or .className:
First-to-bat kid
Sometimes, you just need the first-born child, extract them with :first
pseud-ocity:
Point-blank kid selectors
To pick up direct descendants with a particular attribute, use the direct offspring combinator >
in your find()
:
Sibling selection
Should your jQuery sense tingle for the next sibling, .next()
is thy saving grace:
Practices for the pros
Master the chaining
Make your code concise and readable with deft chaining of jQuery methods and operate your DOM navigation like a pro:
Fine-tuning the selection
Employ .filter()
with .children()
to refine the selection like a picky recruiter:
Deciphering this
Remember, this
in the realm of jQuery event handlers is bound to the element that triggered the event, giving you context for adequate DOM operations.
Context matters
Consider using more contextual selectors to prevent accidently calling unexpected elements from elsewhere in the DOM:
Thinking performance
Be aware that .find()
may not be the sprinter in a large DOM marathon. Use more specific selectors to unburden your jQuery object agent.
Was this article helpful?