Gab-1997 Gab-1997 - 1 year ago 77
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!

Answer Source

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";

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download