jared-shepherd of Nullera
11/1/2018 - 8:09 AM

JS - DOM - Insert After Element

Use this code to insert an element after a referenced element in pure JS.

function insertBefore(newElement, referenceNode) {
  referenceNode.parentNode.insertBefore(newElement, referenceNode.nextSibling);
}

// example usage
var newElem = document.createElement('div');
newElem.innerHTML = '<p>Im new here!</p>';

var refElem = document.querySelector('.referenceElement');

insertBefore(newElem, refElem);