Add class with Javascript - Javascript DOM

Javascript examples for DOM:Element className

Description

Add class with Javascript

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <style id="compiled-css" type="text/css">

#my/*from w w  w. ja v  a 2 s  .  com*/
{
   width: 100px; height: 100px; border: 1px solid #000;
}
.class1
{
   background-color: #0e0e0e;
}


      </style> 
      <script type="text/javascript">
    window.onload=function(){
document.getElementById('my').className = "class1";
    }

      </script> 
   </head> 
   <body> 
      <div id="my"></div>  
   </body>
</html>

Related Tutorials