views:

97

answers:

4

I am trying to retrieve the parent of an element's parent (grandparent?). I want to find that grandparent, then find a 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);
    });

Thanks!

+5  A: 

try:

$(this).parent().parent().find('.thingtofind').text();
MDCore
I thought I already tried that, but I guess not! Works great, thanks.
j-man86
+1  A: 
$('.mixPlayCell a').click(function() {
  elem = $(this).parent().parent();
  title = $("tag selector goes here",elem).html();
});

Something like this?

Jeriko
+1, .get(0) is unnecessary and returns the DOM object, which doesn't have the jQuery methods. [See here](http://static.bwerp.net/~adam/2010/04/20/parent.html).
Adam Backstrom
A: 
$(this).parent().parent().find('mytag').text()

Should work just fine.

You can use any selector in the find method. If you'll just need to search in the direct children of the grandparent's element, you can also use $.children instead of find which searches in all of the element's children.

Jan Jongboom
A: 

There is only 1 parent, no need to .get(0)

    var title = $(this).parent().parent().find("whateveryouwant").html();
marcgg