2

I have several checkbox fields which are nested in table td's like following:

<td>
    <div class="am-checkbox ChangeCheckbox">
        <input id="check20" type="checkbox" checked="checked">
        <label for="check10"> </label> 
     </div>
</td>

Now all other checkboxes have same structure as well.... Now what I'd like to do here is to trigger a click event to change the state of checkbox and check/uncheck it when it's clicked on it...

What I have done here via jQuery is like following:

$(document).on("click", ".ChangeCheckbox", function () {
    // $('#check20').attr('checked', false);
    // console.log($(this).closest("tr"));
    // console.log($(this).closest("tr").find('td:eq(2)'));
    $(this).closest("tr").find('td:eq(2)').attr('checked', false);
});

I'm trying to find a row which triggered the event, and then it's column #2 (eq(2)) and this is the column where the checkbox is placed...

Now the only problem here that I don't know how to solve is how do I access the input checkbox type after selected second column like following:

// how to select now the checkbox and set it's attribute to unchecked?
$(this).closest("tr").find('td:eq(2)') 

Can someone help me out?

5
  • @Tushar I have added it, it's in the onclick event... Commented Oct 3, 2017 at 12:57
  • why dont use class instead of finding in a row? Commented Oct 3, 2017 at 12:58
  • 1
    $(this).closest("tr").find('td:eq(2)').find(":checkbox").attr("checked", false) api.jquery.com/checkbox-selector Commented Oct 3, 2017 at 12:59
  • eq is zero-based index. Use eq(1) to select second item from set. Also to change the checked state of checkbox, use .prop('checked', true). Commented Oct 3, 2017 at 13:00
  • @freedomn-m yes yes yes that's it , thank you so much !!! =) Commented Oct 3, 2017 at 13:00

2 Answers 2

2

You can use :checkbox pseudo-selector. http://api.jquery.com/checkbox-selector

In your case, this would be:

$(this).closest("tr")
       .find('td:eq(2)')
       .find(":checkbox").att‌​r("checked", false);

Edit

To toggle, it's easiest to get the checkbox first and then just use ! against the current value:

var chk = $(this).closest("tr")
                 .find('td:eq(2)')
                 .find(":checkbox");

chk.prop("checked", !chk.prop("checked"));

(we should be using .prop for checkbox checked, not .attr)

The next issue you'll find is that if you click the checkbox directly it doesn't change - because the checkbox and the tr get the click events, cancelling each out. You'll need to cancel the tick on the checkbox.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey freedom, how would I now combine this with a if statement to see if the checkbox is checked, if it isn't , i'd simply uncheck it, otherwise check it?
You mean you want to toggle the value? I was wondering why you always cleared it. I'll add an edit.
1

Use parents like this:

$(this).parents("tr:first").find('td:eq(2) > input[type="checkbox"]').attr('checked', false);

4 Comments

how do I access the input checkbox - this [didn't] answer the question
Sorry @freedomn-m, I forgot to update the answer for checkbox. I just updated it.
Why did you change the closest("tr") to parents("tr:first")?
I am more familiar with parents compared to closest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.