Gab-1997 Gab-1997 - 4 months ago 16
HTML Question

How to write JavaScript code to duplicate HTML elements?

Suppose I have an HTML element (like a

<div>
). 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

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

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

Comments