Add CSS class name to first child element - Javascript DOM

Javascript examples for DOM:Element firstElementChild

Description

Add CSS class name to first child element

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">

.current {//from   w ww  .java2  s .  c  o m
   color: red;
}


      </style> 
      <script type="text/javascript">
    window.onload=( function() {
document.getElementsByTagName('nav')[0].firstChild.className = "current";
    });

      </script> 
   </head> 
   <body> 
      <nav>
         <a>test</a>
      </nav>  
   </body>
</html>

Related Tutorials