JavaScript node.insertAfter() Method

You are Here:

JavaScript node.insertAfter() Method

The node.insertAfter() method inserts a new node after the reference node (specified node).

Note: The node.insertAfter() is not an inbuilt javascript method, instead we created a user-defined function.


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


The following is just a calling of user-defined function (insertAfter()). For better understanding, we coded it to very much look like the syntax of node.insertBefore().

insertAfter(newNode, refNode)

Parameter Values

newNodeRequiredThe node to be inserted.
refNodeRequiredThe node after 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 insertAfter(newNode, refNode){ refNode.parentNode.insertBefore(newNode, refNode.nextSibling); } function myFunction(){ var newNode = document.createElement("li"); var textnode = document.createTextNode("Mango"); newNode.appendChild(textnode); insertAfter(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