append before javascript 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: javascript add div before element
var my_elem = document.getElementById('my_id');
var span = document.createElement('span');
span.innerHTML = '*';
span.className = 'asterisk';
my_elem.parentNode.insertBefore(span, my_elem);
Example 3: js insertbefore
let insertedNode = parentNode.insertBefore(newNode, referenceNode)
Example 4: js insert before
const alert_box = document.createElement("div")
alert_box.innerText = "Not allowed!";
alert_box.className = "alert";
const cont = document.querySelector(".container");
const prof = document.getElementById("profile");
cont.insertBefore(alert_box, prof);
Example 5: javascript add element above
$( "<p>Test</p>" ).insertBefore( ".inner" );