The ParentNode.prepend() method inserts a set of Node objects or DOMString objects before the first child of the ParentNode. Hence, child Node is set at 0th index of Node Object List.
ParentNode.prepend( ChildNodesToPrepend );
- ChildNodesToPrepend: Child nodes to prepend acts as Parameter for this method.
- prepend text: We can prepend text also.
Return Value: This method returns undefined.
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 the Child1 and Child2 to parentNode.
In console we had shown childNodes of parentNode.
In console, you can see childNodeList of parentNode. One is div and one is p
Example 2:Prepending text. In this example, we have prepended some text to innerHTML of the element and the element’s textContent.
In console, you can see textContent of parent element.