The Element.before() method inserts one or more nodes or strings into the children list of this element’s parent, immediately before this element (MDN). Strings become Text nodes. It returns undefined.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call el.before. Start with this line.
JavaScript
el.before(node1, node2, …)
Call
What it does
el.before(node1, node2, …)
Primary signature.
el.before(...)
Call with the arguments your case needs.
Built into the browser. Call before(...) on an Element — no import required.
Library declarations
JavaScript
el.before(node1, node2, …)
Parameters
Parameter
Description
Reference element
Required. Must have a parent to insert into
param1…paramN
Required. Node objects and/or strings
Return Value
Return Value
Description
*
Always undefined
JavaScript Examples
Example 1 — Insert an Element
Place a new sibling right before a paragraph.
JavaScript
const p = document.getElementById("p");
const out = document.getElementById("out");
const span = document.createElement("span");
span.textContent = "before";
p.before(span);
out.textContent = p.parentNode.innerHTML;
Pass several items. Nodes and strings can mix in one call.
Keep argument order. First argument is farthest left before p.
One DOM update pattern. Cleaner than several separate inserts.
Verify. Parent HTML shows span, then text, then p.
Compatibility
Browser Support
Element.before() is Baseline Widely available on MDN (across browsers since April 2018). It is not Deprecated, Experimental, or Non-standard. Prefer it over older sibling-insert patterns when you can.
✓ Baseline Widely available
Element.before()
Modern ChildNode insert — nodes or strings before a sibling. Widely supported since 2018.
2018+Widely available
Google Chrome54+
Supported
Mozilla Firefox49+
Supported
Apple Safari10+
Supported
Microsoft Edge17+
Supported
Opera41+
Supported
Internet ExplorerNot supported — use insertBefore
Unavailable
before()Baseline
Bottom line: Safe for modern browsers. For very old IE, use parent.insertBefore with a reference sibling.
Frequently Asked Questions
It inserts one or more Node objects or strings into the children of this element’s parent, immediately before this element (MDN). Strings become Text nodes.
No. MDN marks Element.before() as Baseline Widely available (across browsers since April 2018). It is not Deprecated, Experimental, or Non-standard.
Nothing useful — the return value is undefined. The DOM is updated in place; check parent.outerHTML or previousSibling if you need to verify the result.
Yes. before(node1, node2, …) inserts all arguments in order, right before the element you call it on.
before() inserts siblings immediately before the element you call it on. after() inserts immediately after. Both are ChildNode helpers; insertBefore() is the classic parent-based API.
If the element is not in a parent’s child list, before() has nowhere to insert. Invalid hierarchy cases can throw a HierarchyRequestError DOMException.