Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
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

 $('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');
   }
 });
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

 $('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');
   }
 });
 
 const parentInputs = $('#parent-tr input')
 
 
 const checkChildRows = (parentInput, isChecked) => {
   const parentRow = parentInput.closest('#parent-tr');
   let childRow = parentRow.nextElementSibling
   
   while(childRow && !(childRow.id === 'parent-tr')) {
     console.log('run')
     childRow.querySelector('input').checked = isChecked
     childRow = childRow.nextElementSibling;
   }
 }
 
 [...parentInputs].forEach(input => input.addEventListener('change', (event) => {
  checkChildRows(input, event.target.checked)
 }))
 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
    <tbody>
        <!-- parent -->
        <tr id="parent-tr">
            <td><input type="checkbox" name="subject" id="subject">Parent</td>
            <td colspan="3"></td>
        </tr>
        <!-- child -->
        <tr id="child-tr">
            <td><input type="checkbox" name="ticker" id="ticker">Child</td>
        </tr>
        <tr id="child-tr">
            <td><input type="checkbox" name="ticker" id="ticker">Child</td>
        </tr>
        <tr id="child-tr">
            <td><input type="checkbox" name="ticker" id="ticker">Child</td>
        </tr>

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

</table>

about 4 years ago · Juan Pablo Isaza Relatório

0

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>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda