
Enter a search term above to see results...
Enter a search term above to see results...
The DOM Traversal methods allow you to get from one element to another based off their relationship. Query supports piercing shadow DOM boundaries when using $$
Finds descendant elements that match the specified selector.
$('selector').find(childSelector)
Name | Type | Description |
---|---|---|
childSelector | string | A selector to match descendant elements |
Query object containing the matched elements.
// Find all paragraphs within a div$('div').find('p').addClass('highlight');
$$('my-component').find('p').addClass('highlight');
Gets the parent of each element in the current set of matched elements.
$('selector').parent(parentSelector)
Name | Type | Description |
---|---|---|
parentSelector | string | (Optional) A selector to filter parent elements |
Query object containing the parent elements.
// Get the parent of a button$('button').parent().addClass('button-group');
$$('my-component menu-item').parent().addClass('menu-styling');
Gets the children of each element in the set of matched elements.
$('selector').children(childSelector)
Name | Type | Description |
---|---|---|
childSelector | string | (Optional) A selector to filter children elements |
Query object containing the child elements.
// Get all direct child list items of an unordered list$('ul').children('li').css('color', 'red');
Gets the siblings of each element in the set of matched elements.
$('selector').siblings(siblingSelector)
Name | Type | Description |
---|---|---|
siblingSelector | string | (Optional) A selector to filter sibling elements |
Query object containing the sibling elements.
// Get all siblings of a selected element$('.selected').siblings().addClass('dimmed');
$$('my-component').find('.shadow-selected').siblings().addClass('shadow-dimmed');
Finds the closest ancestor element that matches the selector, or optionally all matching ancestors.
$('selector').closest(ancestorSelector)
$('selector').closest(ancestorSelector, { returnAll: true })
Name | Type | Description |
---|---|---|
ancestorSelector | string | A selector to match ancestor elements |
options | object | (Optional) Options to control behavior |
options.returnAll | boolean | If true, returns all matching ancestors instead of just the closest |
Query object containing the closest matching ancestor, or all matching ancestors if returnAll: true
.
// Find the closest form ancestor$('input').closest('form').submit();
// Find all div ancestors$('span').closest('div', { returnAll: true }).addClass('ancestor');
$$('my-component').find('input').closest('.shadow-form').addClass('validated');
Gets all ancestor elements that match the selector, traversing up the entire DOM tree. This is a convenient alias for closest(selector, { returnAll: true })
.
$('selector').closestAll(ancestorSelector)
Name | Type | Description |
---|---|---|
ancestorSelector | string | A selector to match ancestor elements |
Query object containing all matching ancestor elements.
// Find all container ancestors$('span').closestAll('.container').addClass('highlight');
// Find all form ancestors for all inputs$('input').closestAll('form').addClass('has-inputs');
// Find all shadow DOM containers$$('input').closestAll('.shadow-container').addClass('shadow-highlight');
pierceShadow
option for shadow DOM traversalGets the immediately following sibling of each element in the set of matched elements.
$('selector').next(nextSelector)
Name | Type | Description |
---|---|---|
nextSelector | string | (Optional) A selector to filter the next element |
Query object containing the next sibling elements.
// Get the next sibling of a paragraph$('p').next().css('margin-top', '20px');
$$('my-component').find('.shadow-paragraph').next().css('margin-top', '10px');
Gets the immediately preceding sibling of each element in the set of matched elements.
$('selector').prev(prevSelector)
Name | Type | Description |
---|---|---|
prevSelector | string | (Optional) A selector to filter the previous element |
Query object containing the previous sibling elements.
// Get the previous sibling of a heading$('h2').prev().css('color', 'red');
$$('my-component').find('.shadow-heading').prev().css('margin-bottom', '5px');
Returns to the previous set of elements in the traversal chain.
$('selector').find('childSelector').end()
None
Query object that was active before the last traversal method was used.
// Allows working with different levels in the DOM without making multiple selections$('.card') .addClass('highlighted') // Add class to cards .find('.title') // Switch to titles within cards .css('font-weight', 'bold') // Bold the titles .end() // Go back to the card selection .find('.content') // Now find content within cards .html('<p>New content</p>'); // Change the HTML
Reduce the set of matched elements to those that match the selector or pass the function’s test.
$('selector').filter(filterSelector)$('selector').filter(function)
Name | Type | Description |
---|---|---|
filterSelector | string | A selector to filter elements |
function | function | A function to test each element against |
Query object containing the filtered elements.
// Filter paragraphs to only those with class 'highlight'$('p').filter('.highlight').css('background', 'yellow');
// Filter list items to only those with text containing 'important'$('li').filter(function() { return $(this).text().includes('important');}).addClass('priority');
Remove elements from the set of matched elements that match the selector or pass the function’s test.
$('selector').not(notSelector)$('selector').not(function)
Name | Type | Description |
---|---|---|
notSelector | string | A selector to exclude elements |
function | function | A function to test each element against |
Query object containing the remaining elements.
// Select all paragraphs except those with class 'exclude'$('p').not('.exclude').css('color', 'blue');
// Select all list items except empty ones$('li').not(function() { return $(this).text().trim() === '';}).addClass('visible');
Check if any of the matched elements match the given selector.
$('selector').is(testSelector)$('selector').is(function)
Name | Type | Description |
---|---|---|
testSelector | string | A selector to test elements against |
function | function | A function to test each element against |
true
if at least one element matches, false
otherwise.
// Check if any paragraph has class 'active'if ($('p').is('.active')) { console.log('At least one paragraph is active');}
// Check if any input is emptyif ($('input').is(function() { return $(this).val() === '';})) { console.log('At least one input is empty');}
Return the position of the first element within the jQuery object relative to its sibling elements.
$('selector').index()$('selector').index(element)
Name | Type | Description |
---|---|---|
element | string/element/jQuery | (Optional) Element to find index of |
The index position of the element(s).
// Get the index of the first selected elementconst index = $('li.selected').index();console.log(`Selected item is at index: ${index}`);
// Get the index of a specific element within the selectionconst specificIndex = $('li').index($('li.highlight'));console.log(`Highlighted item is at index: ${specificIndex}`);
Check if the first element contains the given element.
$('selector').contains(element)
Name | Type | Description |
---|---|---|
element | DOM element | The element to check for containment |
true
if the element is contained, false
otherwise.
// Check if a div contains a specific paragraphconst $div = $('#container');const $paragraph = $('#content-para');
if ($div.contains($paragraph[0])) { console.log('Container holds the paragraph');}
// Check if any card contains clicked element$('.card').each(function() { if ($(this).contains(event.target)) { console.log('Click happened inside this card'); }});