Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make child tr input checkbox value true in html table using jquery?

Info: I want to make a table with parent row and child row. if the parent row is checked then the child rows make also checked until next parent row is not exits

How to checkbox checked of all child row of parent row if parent row is checked until new parent row is.

<table>
    <tbody>
        <!-- parent -->
        <tr id="parent-tr">
            <td><input type="checkbox" name="subject" id="subject"></td>
            <td colspan="3"></td>
        </tr>
        <!-- child -->
        <tr id="child-tr">
            <td><input type="checkbox" name="ticker" id="ticker"></td>
        </tr>
        <tr id="child-tr">
            <td><input type="checkbox" name="ticker" id="ticker"></td>
        </tr>
        <tr id="child-tr">
            <td><input type="checkbox" name="ticker" id="ticker"></td>
        </tr>

        <!-- parent -->
        <tr id="parent-tr">
            <td><input type="checkbox" name="subject" id="subject"></td>
            <td colspan="3"></td>
        </tr>
        <!-- child -->
        <tr id="child-tr">
            <td><input type="checkbox" name="ticker" id="ticker"></td>
        </tr>
        <tr id="child-tr">
            <td><input type="checkbox" name="ticker" id="ticker"></td>
        </tr>
        <tr id="child-tr">
            <td><input type="checkbox" name="ticker" id="ticker"></td>
        </tr>
    </tbody>

</table>

Jquery.js

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 $('tbody tr#parent-hr').on('change', ':checkbox', function () {
    if ($(this).is(':checked')) {

    var currentRow = $(this).closest('tr#parent-hr');
    var targetedRow = currentRow.nextAll('tr#child-hr');

    var targetedCheckbox = targetedRow.find(':checkbox');
    targetedCheckbox.prop('checked', true).trigger('change');
   }
 });

>Solution :

I give you an example for your reference.

 $('tbody tr.parent-tr').on('change', ':checkbox', function () {
    let tbody=$(this).parents("tbody");
    tbody.children("tr.child-tr").find(":checkbox").attr('checked', $(this).is(':checked'));
 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
    <tbody>
        <!-- parent -->
        <tr class="parent-tr">
            <td>Parent 1<input type="checkbox" name="subject" id="subject"></td>
            <td colspan="3"></td>
        </tr>
        <!-- child -->
        <tr class="child-tr">
            <td>Child of Parent 1<input type="checkbox" name="ticker" id="ticker"></td>
        </tr>
        <tr class="child-tr">
            <td>Child of Parent 1<input type="checkbox" name="ticker" id="ticker"></td>
        </tr>
        <tr class="child-tr">
            <td>Child of Parent 1<input type="checkbox" name="ticker" id="ticker"></td>
        </tr>
    </tbody>
    <tbody>
        <!-- parent -->
        <tr class="parent-tr">
            <td>Parent 2<input type="checkbox" name="subject" id="subject"></td>
            <td colspan="3"></td>
        </tr>
        <!-- child -->
        <tr class="child-tr">
            <td>Child of Parent 2<input type="checkbox" name="ticker" id="ticker"></td>
        </tr>
        <tr class="child-tr">
            <td>Child of Parent 2<input type="checkbox" name="ticker" id="ticker"></td>
        </tr>
        <tr class="child-tr">
            <td>Child of Parent 2<input type="checkbox" name="ticker" id="ticker"></td>
        </tr>
    </tbody>
</table>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading