Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the closest jquery function find child tags inside other tags

I have this HTML code

<td>
<div><p> My Txt  </p></div>
<div><a class="linkclass" >link1 </a><a> link2 </a></div>
</td>

Now i want that if someone click on a.linkclass then i alert the text inside p tag

I tried this but it didn't work

$(this).closest('p').text();
like image 820
Mirage Avatar asked Oct 11 '12 00:10

Mirage


1 Answers

Try this

$(this).closest('div').prev('div').find('p').text();

Here p is not the ancestor of the anchor tag.. Is is nested inside a div.. So you cannot use the .closest() on the p tag directly..

The closest selector only traverses thru the ancestors of the element...

EDIT

You can also use the closest on the td directly.

$(this).closest('td').find('p').text();
like image 191
Sushanth -- Avatar answered Oct 11 '22 12:10

Sushanth --