Learn HTML DOM
Manipulating DOM Elements
4.11
Lesson

Insert HTML Element Using InsertBefore

We can append child elements to another element using the appendChild method. But this way, we can only add the element as the last child of the parent element. What if we want to add an element before a certain element?

In the above example, we have created a new button element and inserted it at a certain position in the page.

The insertBefore method is called on the parent element and takes two arguments:

  • The element that is to be moved or inserted
  • The element before which we want to place the newly positioned element