Reputation: 21749
<table>
<tr>
<td class="iwant">ttt</td>
<td>some text <span class="txt">haha</span></td>
</tr>
</table>
$(".txt").on("click", function() {
alert($(this).closest("td.iwant").html())
})
It just gives undefined. Why not ttt?
edit: i know the solution how to make it work with prev
and parent
, I just want to know why closest
doesn't work.
Upvotes: 1
Views: 1040
Reputation: 78650
You want to get the closest
td
and then get the previous sibling with prev
.
$(this).closest("td").prev(".iwant").html()
Upvotes: 2