create element and insert before an element with element.parentNode.insertBefore - Javascript DOM

Javascript examples for DOM:Element parentNode

Description

create element and insert before an element with element.parentNode.insertBefore

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(){//w  w  w  . j av a 2s  .  com
function wrapElem(el, wrapWith) {
    var newElem = document.createElement(wrapWith);
    el.parentNode.insertBefore(newElem, el);
    newElem.appendChild(el);
}
wrapElem(document.getElementsByTagName('input')[0], 'div');
    }

      </script> 
   </head> 
   <body> 
      <input type="text">  
   </body>
</html>

Related Tutorials