jQuery - Getting the second level parent of an element

HandiworkNYC.com picture HandiworkNYC.com · Apr 20, 2010 · Viewed 59.4k times · Source

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);
    });

Answer

Radek picture Radek · Aug 9, 2011

For selecting a second parent

parents(':eq(1)')

looks smoother than

parent().parent()

Also, you might find that

closest('ul')

fits the job better.