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

103
Views
Javascript cloneNode para agregar nuevos elementos

lo que estoy tratando de hacer es. cuando hago clic

 function runIt(text) { var counter = 1; var comment = document.getElementById("name"); comment.innerText = text; comment.cloneNode(true); comment.id += counter; } document.addEventListener("click", function(e){ runIt("test") }, true);

Quiero que AGREGUE un nuevo elemento debajo de esa "prueba" de salida. se sigue reemplazando. :(

HTML

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>t</title> </head> <body> <p id="name" class="someclass"></p> </body> </html>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

cloneNode devuelve el nuevo código, que luego puedeagregar al DOM. Además, el contador debe definirse fuera de la función y luego incrementarse cada vez.

 var counter = 1; function runIt(text) { var comment = document.getElementById("name"); newcomment = comment.cloneNode(true); newcomment.innerText = text; newcomment.id += counter; counter++; document.querySelector('body').append(newcomment) } document.addEventListener("click", function(e){ runIt("test") }, true);
 <p id="name" class="someclass">-</p>

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!