Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q1441 How can I change the background color of individual table cells randomly at a specified interval utilizing a predefined array of colors?

irt.org | Knowledge Base | JavaScript | Table | Q1441 [ previous next ]

Q1441 How can I change the background color of individual table cells randomly at a specified interval utilizing a predefined array of colors?

Try:

<html>
<head>

<style><!--
.relative { position:relative; }
//--></style>

<script language="JavaScript"><!--
var colors= new Array('#ff0000','#00ff00','#0000ff');

function change(i) {
    var color = colors[Math.floor(colors.length*Math.random())]
    if (document.layers)
        window.document.layers['id' + i].bgColor = color;
    else if (document.all)
        window.document.all['id' + i].style.background = color;
}

function loop() {
  change(Math.floor(9*Math.random()));
  setTimeout('loop()',50);
}
//--></script>

</head>

<body onLoad="loop()">

<table>
<tr>
<td id="id0" class="relative">test</td>
<td id="id1" class="relative">test</td>
<td id="id2" class="relative">test</td>
</tr>
<tr>
<td id="id3" class="relative">test</td>
<td id="id4" class="relative">test</td>
<td id="id5" class="relative">test</td>
</tr>
<tr>
<td id="id6" class="relative">test</td>
<td id="id7" class="relative">test</td>
<td id="id8" class="relative">test</td>
</tr>
</table>

</body>
</html>

Soeren Pedersen writes:

Not really that great since the example breaks for Netscape Navigator as soon as you change the table width. Try setting it to 550+.

Feedback on 'Q1441 How can I change the background color of individual table cells randomly at a specified interval utilizing a predefined array of colors?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 6th July 2009. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2009 irt.org, All Rights Reserved.