Reputation: 467
from Duplicate DIV onclick() I get this code https://codepen.io/paulroub/pen/bxgIe
then I modify for me requeriment (clone ANY node into document):
<div id="repeatTHIS">
<button class="button" onclick="clonarid('repeatTHIS');">text</button>
</div>
<hr />
<a href="#" onclick="clonarid('clonarh');">Clone "hello!"</a>
<div id="clonarh">Hello!</div>
<hr />
more tags/info here...
<script>
var i = 0;
function clonarid(elid){
var original = document.getElementById(elid);
var clone = original.cloneNode(true);
clone.id = elid + ++ i;
original.parentNode.appendChild(clone);
}
</script>
but this ever put "new node" in END of DOC, and not just AFTER of each "id", what line I need add?
thanks
Upvotes: 0
Views: 81
Reputation: 370729
original.parentNode.appendChild
will put the child at the end of the parentNode
, which may not be right after the original
. Use insertAdjacentElement
instead.
original.insertAdjacentElement('afterend', clone);
Upvotes: 1