Yetimwork Beyene
Yetimwork Beyene

Reputation: 2337

How to get the td value from a input:checked using jquery?

For the life of me, I can't figure out how to get the td data using $(this). Here's my attempt

<tbody id="testTable">
    <tr><td class="tdData"><input type="checkbox"></input></td></tr>
</tbody>

$("#testTable tr td > input:checked").each(function()
{
    alert($(this).parent().text()); // alert shows empty
    alert($(this).find(".tdData").text()); // alert shows empty
    alert(this.nodeName); // alert shows INPUT
    alert($(".tdData").text()); // alert shows data
});

Using $(this) does not seems to work.

Upvotes: 0

Views: 282

Answers (2)

jtheman
jtheman

Reputation: 7491

$(this) in an each() loop refers to the element posted in the initial statement, which is $("#testTable tr td > input:checked") ie the checkbox.

So if you like to get the td value of each cell just use

$("#testTable tr td").each(...

then $(this) refers to the table cell. Getting the text value of an input or an empty td won't return anything, as you seen.

Upvotes: 0

palaѕн
palaѕн

Reputation: 73926

$("#testTable tr td > input:checked").each(function()
{
     alert($(this).val()); 
});

Upvotes: 1

Related Questions