Horje
How to insert an element after another element in JavaScript without using a library? Code Example
How to insert an element after another element in JavaScript without using a library?
//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);
How to insert an element after another element in JavaScript without using a library?
//Where referenceNode is the node you want to put newNode after. If referenceNode is the last child within its parent element, that's fine, because referenceNode.nextSibling will be null and insertBefore handles that case by adding to the end of the list.

//So:

function insertAfter(newNode, referenceNode) {
    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
You can test it using the following snippet:

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

var el = document.createElement("span");
el.innerHTML = "test";
var div = document.getElementById("foo");
insertAfter(div, el);
<div id="foo">Hello</div>




Javascript

Related
js get current time minutes Code Example js get current time minutes Code Example
string to number angularjs Code Example string to number angularjs Code Example
javascript insert an element after another element Code Example javascript insert an element after another element Code Example
cb() never called!  npm ERR! This is an error with npm itself. Code Example cb() never called! npm ERR! This is an error with npm itself. Code Example
javascript preload images Code Example javascript preload images Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
6