JavaScript after code example

Example 1: append after javascript

//insert new Element after some reference Element
function insertAfter(newElement, referenceElement) {
    referenceElement.parentNode.insertBefore(newElement, referenceElement.nextSibling);
}

//example usage
var newElement = document.createElement("div");
    newElement.innerHTML = "my New Div Text";
var myCurrentElement= document.getElementById("myElementID");
    insertAfter(newElement, myCurrentElement);

Example 2: JavaScript after

var parent = document.createElement("div");
var child = document.createElement("p"); 
parent.appendChild(child);

child.after("Text"); 

console.log(parent.outerHTML);
// "<div><p></p>Text</div>"

Tags:

Css Example