Sunday, May 10, 2026
HomeLanguagesJavascriptHTML DOM ParentNode.prepend() Method

HTML DOM ParentNode.prepend() Method

The ParentNode.prepend() method inserts a set of Node objects or DOMString objects before the first child of the ParentNode. Hence, the child Node is set at the 0th index of the Node Object List.

Syntax:

ParentNode.prepend( ChildNodesToPrepend );

Parameters: 

  • ChildNodesToPrepend: Child nodes to prepend act as Parameters for this method.
  • prepend text: We can prepend text also.

Return Value: This method returns undefined.

The below examples illustrate the ParentNode.prepend() method:

Example 1: Prepending an element. To show this method, we have created three elements parentNode, Child1 and Child2. Then we prepended Child1 and Child2 to parentNode.In the console, we had shown childNodes of parentNode.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <title>Prepend</title>
</head>
 
<body>
    <h1>neveropen</h1>
 
    <script>
        let parentNode = document.createElement("div");
        let Child1 = document.createElement("p");
        let Child2 = document.createElement("div");
        parentNode.prepend(Child1);
        parentNode.prepend(Child2);
        console.log(parentNode.childNodes);
    </script>
</body>
 
</html>


Output: In the console, you can see the childNodeList of parentNode.One is div and one is p

 

Example 2:Prepending text. In this example, we have prepended some text to the innerHTML of the element and the element’s textContent.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <title>Prepend</title>
</head>
 
<body>
    <h1>neveropen</h1>
 
    <script>
        let parent = document.createElement("div");
        parent.innerHTML =
            "A Computer Science Portal for Geeks";
        parent.prepend("neveropen : ");
        console.log(parent.textContent);
    </script>
</body>
 
</html>


Output: In the console, you can see the textContent of the parent element.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS