user2005657
user2005657

Reputation: 627

how to set a table cell to a value when next sibling td's checkbox is checked

this is my code:

 ...
    <td class="paymentStatus">@Model.paymentStatus</td>
    <td><input type="checkbox" class="checkbox"/></td>
 </tr>

What I'm wanting to do is that when the checkbox is checked set the paymentStatus td text to be "Payment Checked"

I tried this:

$(".checkbox").change(function () {
    if ($(this).is(":checked")) {

        $(this).closest('.paymentStatus').text("Payment Checked");
    }
});

Doesn't work. Anyone know why and how to fix?

Upvotes: 0

Views: 135

Answers (2)

PSL
PSL

Reputation: 123739

You need to use closest to get to its parent td and then to the sibling(td.paymentStatus) to set the text.

Demo

$(".checkbox").change(function () {
    if ($(this).is(":checked")) {
          $(this).closest('td')
          .siblings('td.paymentStatus')
          .text("Payment Checked");
    }
});

Upvotes: 2

Ja͢ck
Ja͢ck

Reputation: 173642

You have to move one level up and then select the previous sibling:

$(this)
    .parent()
    .prev('.paymentStatus')
    .text('Payment checked');

Upvotes: 1

Related Questions