Learn JQuery - Learn JQuery - Traversing The DOM Cheatsheet - Codecademy
Learn JQuery - Learn JQuery - Traversing The DOM Cheatsheet - Codecademy
jQuery .parent
The jQuery .parent() method returns the parent
element of a jQuery object. <ul>ul <!-- this is the parent of li's
one, two, six and ul three -->
<li class="one">li</li>
<li class="two">li</li>
<ul class="three"> <!-- this is the
parent of li's four and five -->
<li class="four">li</li>
<li class="five">li</li>
</ul>
<li class="six">li</li>
</ul>
jQuery .siblings
The jQuery .siblings() method targets all of the sibling
elements of a particular element. $('.choice').on('click', event => {
.siblings() can be used to add a selected class to an // Remove the 'selected' class from any
element on click and remove it from all of its sibling siblings
elements, ensuring that only one element appears as $(event.currentTarget).siblings().remove
“selected” at one time.
Class('selected');
// Adds 'selected' class to that element
only.
$(event.currentTarget).addClass('selecte
d');
});
jQuery .closest
The jQuery .closest() method travels up through the
DOM tree to find the first (and closest) ancestor element
matching a selector string.
jQuery .next
The jQuery .next() method targets the next element
that shares the same parent element as the original <ul>
element. <li class="one">Item one</li>
In the following HTML code, the element returned by <li class="two">Item two</li>
$('.two').next() would be <li class="three">Item <li class="three">Item three</li>
three</li> .
</ul>
jQuery .find()
In jQuery, the .find() method will find and return all
descendent elements that match the selector provided as /*
an argument. In HTML:
This code block shows a snippet of HTML that has a <ul id='shopping-list'>
simple shopping list. Using jQuery, the list items inside the <li class='list-item'>Flour</li>
shopping list can be selected. The listItems variable will
<li class='list-item'>Sugar</li>
be a jQuery object that contains the two list items from
</ul>
the shopping list.
*/
// jQuery:
const listItems = $('#shopping-
list').find('.list-item');