To select direct child elements of $(this) in jQuery, use .children(selector) (or .find("> " + selector) / child combinator ). Unlike .find(selector), which searches all descendants at any depth (children, grandchildren, and deeper), .children() matches only immediate children one level below $(this).
1. Selecting Direct Children with .children()
Pass an optional selector string to .children(selector) to filter immediate children by tag, class, or attribute—or omit the selector to get all direct child elements:
// Select all direct children or filter direct children by selector on $(this)
$(".accordion-item").on("click", function () {
const $allDirectChildren = $(this).children();
const $directPanels = $(this).children(".accordion-panel");
const $directInputs = $(this).children("input[type='checkbox']");
console.log("Direct children count:", $allDirectChildren.length);
console.log("Matching direct panels:", $directPanels.length);
console.log("Matching direct checkboxes:", $directInputs.length);
});
.children()(no arguments): Returns a jQuery collection of all direct element children of$(this)in document order..children(".class")/.children("li"): Filters direct children only—ignoring deeper descendants that happen to share the same class or tag.- Non-element nodes excluded: Like standard element selectors,
.children()returns DOM elements rather than raw text or comment nodes.
2. Practical Example: Scoped State Updates Inside a Component
.children() is especially useful in nested components (like nested menus, cards, or form groups) where .find() would accidentally match elements inside sub-components:
// Toggle active class only on direct child rows of the clicked container
$(".matrix-group").on("click", ".group-header", function () {
const $group = $(this).closest(".matrix-group");
$group
.children(".matrix-row")
.toggleClass("is-expanded");
});
- Prevents cross-nesting leaks: If a
.matrix-rowcontains another nested.matrix-group,$group.childre(".matrix-row")updates only the outer group’s immediate rows. - Pairs well with
.closest(): Walking up to the container with.closest()and down one level with.children()keeps DOM queries tightly scoped.
3. Common Traps to Avoid
Selecting direct children in jQuery can go wrong when depth or context is overlooked:
- Using
.find(".item")when you only want direct children:.find()traverses all nested descendants, so nested.itemelements deeper in the tree get matched too. - Using arrow functions
() => {}when relying onthis: In jQuery event callbacks, arrow functions lexically bindthisinstead of setting it to the event target element—use a standardfunction () {}when referencing$(this). - Assuming
$(this)is already cached: Calling$(this)repeatedly inside loops or chains re-wraps the DOM element; storeconst $el = $(this);when reusing it multiple times.
TL;DR: Use $(this).children() (or $(this).children(".child-selector")) to target only immediate one-level-deep children of $(this) without matching deeper descendants.
Are you filtering direct children by class/tag, scoping updates inside nested components, or reading data/values from direct child elements?
