Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

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

You are here: irt.org | FAQ | JavaScript | Table | Q1441 [ previous next ]

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?'

©2018 Martin Webb