Javascript DOM How to - Insert new created divs into the dom








Question

We would like to know how to insert new created divs into the dom.

Answer


<!DOCTYPE html>
<html>
<head>
</head><!-- w ww.  j a  v a 2 s.  co m-->
<body>
  <script type='text/javascript'>
window.onload=function(){
    var new_div = document.createElement("div");
    var app_div = document.getElementById("app");
    document.body.insertBefore(new_div, app_div);

} 
</script>
</body>
</html>

The code above is rendered as follows: