Languages
[Edit]
EN

JavaScript - set DOM element as first child

0 points
Created by:
DEX7RA
580

In this article, we would like to show you how to set the DOM element as the first child using JavaScript.

Practical examples

Edit

Example 1 - first child of the body

Edit

In this example, we use insertBefore() method to set the newFirstChild element to be the first child of the body element.

Example 2 - working with container

Edit

In this example, we use firstChild property to get the first child of the container. Then we use insertBefore() method to set the newFirstChild element to be the first child of the container.

 

References

Edit
  1. Node.insertBefore() - Web APIs | MDN

Alternative titles

  1. JavaScript - set HTML DOM element as first child
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join