Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

307
Views
Después de agregar por primera vez, javascript no agrega un elemento clonado por segunda vez

En mi aplicación estoy agregando elementos clonados:

 document.getElementById("add").addEventListener("click", function(){ let theblock = document.getElementById("theblock").cloneNode(true); let newer = theblock; newer.removeAttribute("id"); document.getElementById("restblocks").append(newer);

Un poco si clono fuera del alcance, agrega el elemento a html solo una vez:

 let theblock = document.getElementById("theblock").cloneNode(true); document.getElementById("add").addEventListener("click", function(){ let newer = theblock; newer.removeAttribute("id"); document.getElementById("restblocks").append(newer);

¿Cuál podría ser la razón?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Porque cuando clona afuera, solo hace una sola clonación, y un Nodo solo puede existir en un lugar en un documento. Si desea evitar la consulta dentro del controlador de clics, puede consultar afuera, pero clonar adentro.

 let theblock = document.getElementById("theblock"); document.getElementById("add").addEventListener("click", function(){ let newer = theblock.cloneNode(true); ... }); 

 const theblock = document.getElementById('theblock'); const restblocks = document.getElementById('restblocks'); document.getElementById('add').addEventListener('click', function () { let newer = theblock.cloneNode(true); newer.removeAttribute('id'); newer.classList.remove('hidden'); restblocks.append(newer); });
 .block { width: 40px; height: 40px; margin: 4px; background-color: pink; } .hidden { display: none; }
 <button type="button" id="add">Add a block</button> <div id="restblocks"></div> <div id="theblock" class="block hidden"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!