const container = document.createElement("div");
const p = document.createElement("p");
container.appendChild(p);
const span = document.createElement("span");
p.before(span);
console.log(container.outerHTML);
// "<div><span></span><p></p></div>"Element: before() method / notebook
Element: before() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const container = document.createElement("div");
const p = document.createElement("p");
container.appendChild(p);
p.before("Text");
console.log(container.outerHTML);
// "<div>Text<p></p></div>"const container = document.createElement("div");
const p = document.createElement("p");
container.appendChild(p);
const span = document.createElement("span");
p.before(span, "Text");
console.log(container.outerHTML);
// "<div><span></span>Text<p></p></div>"