Stackoverflow
Stackoverflow

Reputation: 467

clone node just after of itself (not in any place...)

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

Answers (1)

CertainPerformance
CertainPerformance

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

Related Questions