Languages
[Edit]
EN

JavaScript - Node insertBefore() method example

0 points
Created by:
Giles-Whittaker
739

In this article, I would like to present documentation of the Node.insertBefore() method.

The method inserts a node before referenceNode as a child of a specified parentNode.

Quick overlook:

Note:

If the given node already exists, insertBefore() moves it from its current position to the new one.

 

1. Documentation

Edit
SyntaxparentNode.insertBefore(newNode, referenceNode)
Parameters
  • parentNode - the parent of the newly inserted node,
  • newNode - the node to be inserted,
  • referenceNode - the node before which newNode is inserted.
Result

The method returns the added child.

DescriptionThe method inserts a node before referenceNode as a child of a specified parentNode.

Note:

If the referenceNode is null, then newNode is inserted at the end of parentNode.

2. Insert before indicated element (between elements)

Edit

In this example, we will insert a new paragraph before child-2 element.

3. Insert as first element

Edit

In this example, we will insert a new paragraph before all child elements inside the parent.

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - DOM Node documentation

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join