Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Getting the second level parent of an element

I am trying to retrieve the parent of an <a> element's parent (grandparent?). I want to find that grandparent, then find a <b> tag in that element, then store that text as a variable to publish elsewhere on the page. I've been trying to use the parent() function but without success.

Here's the code I tried:

    $('.mixPlayCell a').click( function() {         var title = $(this).parent().get(0).parent().get(0).text();         alert(title);     }); 
like image 346
HandiworkNYC.com Avatar asked Apr 20 '10 13:04

HandiworkNYC.com


People also ask

How do I find a specific parent in jQuery?

The parents() is an inbuilt method in jQuery which is used to find all the parent elements related to the selected element. This parents() method in jQuery traverse all the levels up the selected element and return that all elements.

What does parent () do in jQuery?

The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.

How do you find the parent element?

To get the parent node of an HTML element, you can use the parentNode property. This property returns the parent node of the specified element as a Node object. The parentNode property is read-only, which means you can not modify it.

How do I get grandparents in jQuery?

jQuery parentsUntil() Method The parentsUntil() method returns all ancestor elements between the selector and stop. An ancestor is a parent, grandparent, great-grandparent, and so on.

How do I get the parent of an element in jQuery?

.parent() By using the jQuery .parent() method, you can traverse one level up the DOM tree and return the direct parent of a specified element. It is also possible to add a selector expression ($()).

How to get second child element of list in jQuery?

In this tutorial, learn how to get second child element of list in jQuery. The short answer is: use the jQuery nth-child () selector to select the second child item. You can also use the jQuery eq () method to select the second item of the list element. Let’s find out with the examples given below.

What is the difference between parent and parent () methods in jQuery?

Given a jQuery object that represents a set of DOM elements, the parent() method traverses to the immediate parent of each of these elements in the DOM tree and constructs a new jQuery object from the matching elements. This method is similar to .parents(), except .parent() only travels a single level up the DOM tree.

How to traverse all the levels up the selected element using jQuery?

This parents () method in jQuery traverse all the levels up the selected element and return that all elements. Here selector is the selected element whose all parent need to find. Parameters: It does not accept any parameter.


2 Answers

For selecting a second parent

parents(':eq(1)') 

looks smoother than

parent().parent() 

Also, you might find that

closest('ul') 

fits the job better.

like image 78
Radek Avatar answered Sep 28 '22 10:09

Radek


try:

$(this).parent().parent().find('.thingtofind').text(); 
like image 20
MDCore Avatar answered Sep 28 '22 09:09

MDCore