JavaScript node.insertBefore() Method

You are Here:

JavaScript node.insertBefore() Method

The node.insertBefore() method inserts a new node before the reference node (specified node).

Note: If the given node already exists in the document, insertBefore() moves it from its current position to the new position.


HTML Online Editor
<!DOCTYPE html> <html> <body> <p id="point">End</p> <script> var parentNode = document.getElementsByTagName("body")[0]; var refNode = document.getElementById("point"); var newNode = document.createElement("p"); var textnode = document.createTextNode("Start"); newNode.appendChild(textnode); parentNode.insertBefore(newNode, refNode); </script> </body> </html>


node.insertBefore(newNode, refNode)

Parameter Values

newNodeRequiredThe node to be inserted.
refNodeRequiredThe node before which newNode is inserted.

More Examples


HTML Online Editor
<!DOCTYPE html> <html> <body> <ol><li>Apple</li></ol> <button onclick="myFunction()">Add Mango</button> <script> var parentNode = document.getElementsByTagName("ol")[0]; function myFunction(){ var newNode = document.createElement("li"); var textnode = document.createTextNode("Mango"); newNode.appendChild(textnode); parentNode.insertBefore(newNode, parentNode.childNodes[0]); } </script> </body> </html>


Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author