Search
Learn
Share

Prepend a child element in JavaScript

Published by Profile Image DevDashboard on 24 May, 2017
25 views
In JavaScript, we can very easily use appendChild to add a new element to the end of the current child elements, but there is no prependChild if you instead want to add it to the beginning.

You can do this instead:

var newChild = document.createElement('div');

var parent = document.getElementById("parent");
parent.insertBefore(newChild, parent.firstChild);