1

I'm trying to clone javascript div id with this code

<script>

    var div = document.getElementById('pruebaz2'),
                            clone = div.cloneNode(true); // true means clone  all childNodes and all event handlers
    clone.id = "pruebaz2";
    document.body.appendChild(clone);

</script>

but my clone div id not have any effect in my html page
my original id add a record when a doing a click

my clone id do nothing

how I can clone with same functionality

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

Your code is fine. You just didn't put any content into the cloned div so there was nothing to see.

Also, don't give two elements the same id. That defeats the purpose of it.

Lastly, a "deep" clone does not duplicate event handlers. From MDN:

Cloning a node copies all of its attributes and their values, including intrinsic (in–line) listeners. It does not copy event listeners added using addEventListener() or those assigned to element properties. (e.g. node.onclick = fn) Moreover, for a element, the painted image is not copied.

var div = document.getElementById('pruebaz2'),

clone = div.cloneNode(true); // true means clone  all childNodes and all event handlers
clone.id = "pruebaz3";
clone.innerHTML = "Cloned DIV";
document.body.appendChild(clone);
<div id="pruebaz2">Original DIV</div>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71