appendchild after element code example
Example 1: append after javascript
function insertAfter(newElement, referenceElement) {
referenceElement.parentNode.insertBefore(newElement, referenceElement.nextSibling);
}
var newElement = document.createElement("div");
newElement.innerHTML = "my New Div Text";
var myCurrentElement= document.getElementById("myElementID");
insertAfter(newElement, myCurrentElement);
Example 2: append sibling javascript after first child
function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
Example 3: appendchild element once if element presense in js
<script>
var button = false;
function myfun(){
if ( button === false){
var b1 = document.createElement("BUTTON");
b1.innerHTML = "Click Me";
document.body.appendChild(b1);
return button = true;
}
}
</script>