Random color for <div> text - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Div

Description

Random color for <div> text

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(){//www  . j a v a 2  s . c o  m
var array_ofcolors = ['red', 'blue','orange']
var obj = document.getElementById('foo');
obj.style.color = array_ofcolors[Math.floor(Math.random()*array_ofcolors.length)]
    }

      </script> 
   </head> 
   <body> 
      <div id="foo">
         THIS IS A TEST
      </div>  
   </body>
</html>

Related Tutorials