mweis mweis - 4 months ago 7
Javascript Question

Acess element in a nested table

Im basically trying to print policy when any of the child elements are clicked.
It's a really weird list that I got and I am trying to get it to work but cannot get it to display.

$(".submenu li").click(function() {

alert($(this).text());
alert( $(this).parent().find('li.sub').text());

});


i tried

alert( $(this).parent().find('li.sub').text());
alert($(this).closest('.submenu').closest('a').text());

alert($(this).closest('.submenu').closest('sub').find("a").text());


http://jsfiddle.net/ettnxuxa/

Expected output after clicking any child element:
its alerts just "policy"

Answer

The parent() method only gives you the immediate parent element. To do what you'd like to do, try:

alert($(this).parent().parent('.sub').text());

Better though, would be to use:

alert($(this).closest('.sub').text());

To get a child element of .sub you would then use the children() method, so:

alert($(this).closest('.sub').children('a').text());

jsfiddle

Comments