Add class when element is created - Javascript DOM

Javascript examples for DOM:Element className

Description

Add class when element is created

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   w  w  w .java2s  .  c o  m


var divElement = document.createElement('div')
divElement.className = 'foo';
divElement.innerHTML = "foo";
document.body.appendChild(divElement);


    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials