Skip to content

Instantly share code, notes, and snippets.

@cgeglio
Last active December 13, 2019 17:50
Show Gist options
  • Save cgeglio/ac8b676488962bc5063612d3f30b15d1 to your computer and use it in GitHub Desktop.
Save cgeglio/ac8b676488962bc5063612d3f30b15d1 to your computer and use it in GitHub Desktop.
jquery-traversing

Describe what DOM traversal is and why it is useful.

jQuery traversing, which means "move through", used to "find" (or select) HTML elements based on their relation to other elements. Start with one selection and move through that selection until you reach the elements you desire.

What can the siblings(), parent(), and children() methods do?

The siblings method allows you to get the siblings of the selected element, ditto for parent and children.

What is prepend(), append(), and what are their differences?

The .append() method inserts the specified content as the last child of the element. The .prepend() method inserts the specified content as the first child of the element.

What is the difference between parent() and parents()? Why would I want to use either?

Parent() method selects one element up (single level up) the DOM tree. Parents() method is similar to parent() but selects all the matching elements up the DOM tree. Begins from the parent element and travels up.

Describe closest() and find(). What are their use cases?

Closest: get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree. Find: Get the descendants of each element in the current set of matched elements, filtered by a selector, by traversing down the DOM tree

Extra Info: The difference between parents() & closest():

Parents() Begins with the parent element Travels up the DOM tree and returns all ancestors that matches the passed expression The returned jQuery object contains zero or more than one element

Closest() Begins with the current element Travels up the DOM tree and returns the first ancestor that matches the passed expression The returned jQuery object contains zero or one element

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment