diff --git a/docs/create-modify-element.mdx b/docs/create-modify-element.mdx index caeef89..c62c85c 100644 --- a/docs/create-modify-element.mdx +++ b/docs/create-modify-element.mdx @@ -109,7 +109,7 @@ ele.setAttribute("type", "text") ### 3. appendChild() -It inserts a new element as the last child of the current element. +It inserts a new element as the last child of the parent element. `Element.appendChild(newElement)` @@ -121,7 +121,7 @@ eleDiv.appendChild(eleH1) //

``` - + If newElement is already a child of another element, then it is removed from its existing parent and then added to the new parent. @@ -164,3 +164,29 @@ parentEle.removeChild(childEle) ``` + +### 5. insertBefore() + +`Element.insertBefore(newEle, refEle)` + +This method inserts a new child element before another child within a parent. + +`newEle` - The node to be inserted. + +`refEle` - The reference element before which newEle should be inserted. If refEle is `null`, then newEle is added as the last child. + +```js +//
    +//
  • Apple
  • +//
  • Zebra
  • +//
+ +// To add a new
  • above "Zebra" + +var parent = document.querySelector("ul") +var ref = document.querySelector("li:last-child") + +var newEle = document.createElement("li") + +parent.insertBefore(newEle, ref) +``` \ No newline at end of file