Jump to content

How to keep changing a text's color ?


noname

Recommended Posts

:) hi, I've just begun to learn java script , i have a exercises, its description : "You should keep changing a text's color". I have a array to containt colors, but when i use setTimeout() and document.write() to display the text, have a error: Object expected. Can you help me to slove this proplem ? :) thanks :)

Link to comment
Share on other sites

<HTML><HEAD><TITLE> Text </TITLE><script type="text/javascript">var colors=new Array();colors[0]="blue";colors[1]="green";colors[2]="red";colors[3]="grey";colors[4]="orange";colors[5]="aqua";colors[6]="brown";colors[7]="black";colors[8]="purple";colors[9]="chocolate";colors[10]="magenta";colors[11]="violet";colors[12]="indigo";colors[13]="sienna";colors[14]="yellow";var i=0;var t;function writemess(){ if(i==colors.length) return; document.write(str.fontcolor(colors[i++])); t=setTimeout('writemess()',200); //writemess(); }</script></HEAD><BODY onload="writemess()"> </BODY></HTML>

Link to comment
Share on other sites

Patched up :)

<HTML><HEAD><TITLE> Text </TITLE><script type="text/javascript">var colors=new Array();colors[0]="blue";colors[1]="green";colors[2]="red";colors[3]="grey";colors[4]="orange";colors[5]="aqua";colors[6]="brown";colors[7]="black";colors[8]="purple";colors[9]="chocolate";colors[10]="magenta";colors[11]="violet";colors[12]="indigo";colors[13]="sienna";colors[14]="yellow";var i=0;function writemess(){   if (i==15)i=0;  document.getElementById('name').innerHTML=colors[i];  i++;  setTimeout('writemess()',200)}</script></HEAD><BODY onload="writemess()"><div id="name"></div></BODY></HTML>

Yeah like blue says that didn't exist plus i wouldn't use document.write()

Edited by scott100
Link to comment
Share on other sites

I want to keep changing a text's color, but it display many text with another colors

You can do that too, just changedocument.getElementById('name').style.color=colors;and put some text between the div
<HTML><HEAD><TITLE> Text </TITLE><script type="text/javascript">var colors=new Array();colors[0]="blue";colors[1]="green";colors[2]="red";colors[3]="gray";colors[4]="orange";colors[5]="aqua";colors[6]="brown";colors[7]="black";colors[8]="purple";colors[9]="chocolate";colors[10]="magenta";colors[11]="violet";colors[12]="indigo";colors[13]="sienna";colors[14]="yellow";var i=0;function writemess(){   if (i==15)i=0;  document.getElementById('name').style.color=colors[i];  i++;  setTimeout('writemess()',200)}</script></HEAD><BODY onload="writemess()"><div id="name">Text</div></BODY></HTML>

ps it's spelt gray :)

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