Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
Why does my cloning JS code is messing up whole HTML table?

I just wanted to add button that will make a new table under the first one (max. 10 times), but the JavaScript code that I implemented don't do that as I want. Below is my HTML & 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 Respuestas
Responde la pregunta

0

You need to append the copied table to the document body, not back to the table:

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda