0

I have a table inside a table. The basic structure is as follows

<input type="checkbox" name="CheckAll" id="CheckAll"/> <b>Click to select all Option Values</b>
<table class="Parent">
  <tr>
     <td><input type="checkbox" id="Parent">Parent1</td>
     <td><table class="Children">
        <tr><td><input type="checkbox" id="Child">Child1</td></tr>  
        <tr><td><input type="checkbox" id="Child">Child2</td></tr>
        <tr><td><input type="checkbox" id="Child">Child3</td></tr>
     <td>
  </tr>
  <tr>
     <td><input type="checkbox" id="Parent">Parent2</td>
     <td><table class="Children">
        <tr><td><input type="checkbox" id="Child">Child1</td></tr>  
        <tr><td><input type="checkbox" id="Child">Child2</td></tr>
     <td>
  </tr>

When I click Check All, the checkbox above the table, I want all the Check boxes to be selected or deselected. Which I did with the following

    $(document).ready(function () {
        $("#CheckAll").change(function () {
            $("input:checkbox").prop('checked', $(this).prop("checked"));
        });
    });

Now I am trying to select all Children when one of the parent is selected. Suppose I do not have access to the parent id in my View. How can I access only the table that is adjacent to the checkbox that is being changed?

4
  • same ids????????? invalid markup....;) Commented Feb 15, 2016 at 12:36
  • The best possible id I can give is the name of the parent itself. Commented Feb 15, 2016 at 12:36
  • Change de id property to class Parent and you can selector by .Parent[type=checkbox] Commented Feb 15, 2016 at 12:41
  • @Joaquinglez: i will change it to class.. and use adeneo's answer Commented Feb 15, 2016 at 12:42

2 Answers 2

3

You can do that by targeting the next TD, and then table it contains.
Note that you have to change #Parent, #Child etc to classes, as you can't have duplicate ID's.

$('.Parent[type=checkbox]').on('change', function() {
    $(this).closest('td')
           .next('td')
           .find('table input[type=checkbox]')
           .prop('checked', this.checked);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Regarding your note about ids, you can have duplicates if you can still sleep at night knowing you've created invalid html. You could change your selector to work with the duplicates (I believe if you use the "attribute equals" selector to specify id it will work, even though the #id selector won't).
Vini - Good. Certainly classes are the way to go. (I just put the note above because sometimes the person writing JS for a project isn't allowed to change the html that somebody else has created.)
@nnnnnn - that is true, duplicate ID's usually won't break anything, it's just invalid, like a href on a span and many other things one could do. $('[id=Parent]') would get you all the elements, it's just getElementById that only gets the first elements, which jQuery also uses internally when an ID is passed as a selector. Of course, if one can change the HTML, using classes is the better option.
1

Try this,

$(this).parent().next('td').find(':checkbox').prop('checked', this.chekced);

As a side note you have more then elements with same id i.e. Parent which is wrong. You must have unique ids. Instead of giving same id you can use class Parent.

3 Comments

What if i give the name of the parent as the ID? Will it work? In that case what do i write in the jquery?
Instead of giving same id you can use class Parent
Yes. i have changed it to class. thanks for your answer

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.