Insert before an element - Javascript DOM

Javascript examples for DOM:Element insertBefore

Description

Insert before an element

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {//from   www .ja  v  a 2s  .c om
test()
    });

      </script> 
   </head> 
   <body>   
      <script type="text/javascript">
function test()
{
    var newDiv = document.createElement("div");
    newDiv.innerHTML = "<h1>blah blah</h1>";
    document.body.insertBefore(newDiv, document.getElementById('DOMTester'));
}

      </script>   
      <input id="DOMTester" type="submit" onclick="test()">    
   </body>
</html>

Related Tutorials