Gab-1997 Gab-1997 - 3 months ago 12
HTML Question

How to write JavaScript code to duplicate HTML elements?

Suppose I have an HTML element (like a

). Is there a way to duplicate it after a click using JavaScript?

If yes, is it possible to change the content of the duplicated HTML element to something else?

EDIT: Yes, I was asking how to do it as well. Thanks to the guys who answered my question!


You can use Node.cloneNode() on original element, set Elment.innerHTML of cloned node.

<a href="#">a</a>
  var clone = document.querySelector("a").cloneNode();
  clone.innerHTML = "b";