good_evening
good_evening

Reputation: 21749

closest() for getting a html value from the previous td

http://jsfiddle.net/twkRY/

<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

Answers (1)

James Montagne
James Montagne

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

Related Questions