Jump to content

Random Color Text


Recommended Posts

I have some code (adapted from a couple different tutorials, not on here), to make the color of an object randomly change (In this case, the H1). But it isn't working for some reason, even with no syntax errors in DW.

<h1 id="onlytext">Site Header</h1><script>setInterval( document.getElementById('onlytext').style.color = "'rgb('+Math.floor(Math.random()*255)+','+Math.floor(Math.random()*255)+','+Math.floor(Math.random()*255)+')'" ,100 );</script>

The end result I was trying to get was it randomly changes the color every 100ms.

Edited by cclloyd
  • Like 1
Link to comment
Share on other sites

The double-quotes you've put around the whole thing are interfering. Just remove them. For setInterval, use an anonymous function;

setInterval( function() {  document.getElementById('onlytext').style.color = 'rgb('+Math.floor(Math.random()*255)+','+Math.floor(Math.random()*255)+','+Math.floor(Math.random()*255)+')'},100 ); 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...