Blog>
Snippets

Inserting Elements After a Specific Node

Create a new `<span>` element and insert it after a known reference `<div>` element with the ID `marker`.
// Create a new span element
var newSpan = document.createElement('span');

// Set some content for the new span if desired
newSpan.innerHTML = 'New span content';

// Get the reference div element by its ID
var referenceNode = document.getElementById('marker');

// Insert the new span after the reference div element
referenceNode.parentNode.insertBefore(newSpan, referenceNode.nextSibling);
This code block creates a new <span> element, sets its innerHTML to 'New span content', finds the <div> element with the ID 'marker', and inserts the newly created span right after that <div> in the DOM.