Skip to main content Accessibility Feedback

DOM Injection

Element.prepend()

Insert an element at the beginning of a set elements inside a shared parent. Call the prepend() method on the reference node, and pass in the new node as an argument.

// Create a new element
var newNode = document.createElement('li');
newNode.textContent = 'I am new here.';

// Get the parent node
var referenceNode = document.querySelector('#some-element');

// Insert the new node before the first element in the reference node
referenceNode.prepend(newNode);

Requires a polyfill for IE.