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

170
Visualizações
¿Por qué mi código JS de clonación está arruinando toda la tabla HTML?

Solo quería agregar un botón que creará una nueva tabla debajo de la primera (máximo 10 veces), pero el código JavaScript que implementé no hace eso como quiero. A continuación se muestra mi HTML y JSS.

 function addField(n) { var tr = n.parentNode.parentNode.cloneNode(true); document.getElementById('tbl').appendChild(tr); }
 <table id="tbl" class="table-bordered table-dark"> <thead> <tr> <th scope="colgroup">Godzina rozpoczęcia</th> <th scope="colgroup">Godzina zakończenia</th> </tr> </thead> <tbody> <td style="vertical-align: top;"> <label for="start-date">Data:</label> <input type="date" id="start" name="data-roz"> <br> <label for="start-time">Godzina:</label> <input type="time" id="godzina1" name="godz-roz"> </td> <td style="vertical-align: top;"> <label for="end-date">Data:</label> <input type="date" id="end" name="data-zakon"> <br> <label for="end-time">Godzina:</label> <input type="time" id="godzina2" name="godz-zakon"> </td> </tbody> </table> <input type="button" class="btn btn-primary" value="+" onclick="addField(this);"/>

( https://i.imgur.com/WxV060Y.mp4 )

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Debe agregar la tabla copiada al cuerpo del documento, no volver a la tabla:

 function addField(n) { var tblClone = n.previousElementSibling.cloneNode(true); document.body.appendChild(tblClone ); }
 <table id="tbl" class="table-bordered table-dark"> <thead> <tr> <th scope="colgroup">Godzina rozpoczęcia</th> <th scope="colgroup">Godzina zakończenia</th> </tr> </thead> <tbody> <td style="vertical-align: top;"> <label for="start-date">Data:</label> <input type="date" id="start" name="data-roz"> <br> <label for="start-time">Godzina:</label> <input type="time" id="godzina1" name="godz-roz"> </td> <td style="vertical-align: top;"> <label for="end-date">Data:</label> <input type="date" id="end" name="data-zakon"> <br> <label for="end-time">Godzina:</label> <input type="time" id="godzina2" name="godz-zakon"> </td> </tbody> </table> <input type="button" class="btn btn-primary" value="+" onclick="addField(this);"/>

about 4 years ago · Juan Pablo Isaza Relatório

0

 var count = 1; function addField(n) { if(count<=5){ var table = n.previousElementSibling.children[0].cloneNode(true); document.getElementById("table-group").appendChild(table); count++; } if(count===5){ document.getElementById('addBtn').style.display='none'; } }
 <div id="table-group"> <table class="table-bordered table-dark"> <thead> <tr> <th scope="colgroup">Godzina rozpoczęcia</th> <th scope="colgroup">Godzina zakończenia</th> </tr> </thead> <tbody> <tr> <td style="vertical-align: top;"> <label for="start-date">Data:</label> <input type="date" id="start" name="data-roz"> <br> <label for="start-time">Godzina:</label> <input type="time" id="godzina1" name="godz-roz"> </td> <td style="vertical-align: top;"> <label for="end-date">Data:</label> <input type="date" id="end" name="data-zakon"> <br> <label for="end-time">Godzina:</label> <input type="time" id="godzina2" name="godz-zakon"> </td> </tr> </tbody> </table> </div> <input type="button" id="addBtn" class="btn btn-primary" value="+" onclick="addField(this);"/>

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