HTML | DOM before() method

The before() method is used to insert a set of Node objects or HTML DOMString objects in the children list of the ChildNode’s parent. The element is inserted before the ChildNode that we have mentioned.

Syntax:

ChildNode.before(Node or DOMString)

Parameter: This method accepts single parameter as mentioned above and described below:

  • nodes: It is a set of Node objects or HTML DOMString objects that has to be inserted before the ChildNode.

Example 1: In this example, we will insert an element node to the DOM before the child element.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML | DOM before() method
    </title>
</head>
  
<body>
    <div id="div">
        <h1>GeeksforGeeks</h1>
        <p id="p">Child Node</p>
    </div>
      
    <button onclick="add()">
        click to add
    </button>
      
    <script>
        // Get the parent element
        var parent = document.getElementById("div");
        console.log(parent)
  
        // Get the element to add before
        var para = document.getElementById("p");
  
        // Function to add the element
        function add() {
  
            // Create a new element to add
            const div = document.createElement("div");
            div.innerHTML = "<h4>new node</h4>";
  
            // Insert the created element
            para.before(div);
        }
        console.log(parent.outerHTML);
    </script>
</body>
  
</html>

chevron_right


Output:



In output, it can be seen that after the button is clicked, a new node is inserted before child <p> element.

  • Before Clicking the Button:

  • After Clicking the Button:

Example 2: In this example, we will insert some text before the child node.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML | DOM before() method
    </title>
</head>
  
<body>
    <div id="div">
        <h1>GeeksforGeeks</h1>
        <p id="p">Child Node</p>
    </div>
  
    <button onclick="add()">
        click to add
    </button>
  
    <script>
        // Get the parent element
        var parent = document.getElementById("div");
        console.log(parent)
  
        // Get the element to add before
        var para = document.getElementById("p");
  
        // Function to add the element
        function add() {
  
            // Insert a text element
            // before this element
            para.before(
                "Text Added Before ChildNode");
        }
        console.log(parent.outerHTML);
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before Clicking the Button:

  • After Clicking the Button:

Supported Browsers: The browsers supported by DOM before() method are listed below:

  • Google Chrome
  • Edge
  • Firefox
  • Opera

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.