Jquery get id of great great great great great grandfather
you can also try closest
to get the attribute like this:
$(this).closest('div.Myclass').attr('id');
or the second way
$(this).parents('div.Myclass').attr('id')
see here: http://jsfiddle.net/sKqBL/10/
source to share
Get it all .parents()
and use .eq()
...
$(this).parents().eq(5).attr('id');
... or a selector :eq()
...
$(this).parents(':eq(5)').attr('id');
... or make a function ...
function up(el, n) {
while(n-- && (el = el.parentNode)) ;
return el;
}
... and use it like this:
up(this, 5).id
Is the identifier you are trying to retrieve dynamically and therefore you don't know what it is?
If so, consider assigning a unique CSS class name to your great great grandfather. Then you should be able to do something like this:
$(".MyGreatGreatGreatGrandparentCssClass").attr("id");
Of course, if you do, you may not need a great grandfather.
source to share