frequently ask ? : Computers : Programming : Languages : JavaScript : Tables

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

13 of 18 people (72%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How can I let the background color of some table cells blink?
How can I let the background color of some table cells blink?

Sep 17th, 2001 02:50
Martin Honnen,


Here is example code that works with IE4+ and NN6:
<html>
<head>
<script type="text/javascript">
var id, backgroundColor, currentColor;
var tid;
function startBlink (idPrefix, color) {
  if (document.all || document.getElementById) {
    id = idPrefix;
    backgroundColor = color;
    currentColor = color;
    tid = setInterval('blink()', 500);
  }
}
function blink () {
  var i = 0;
  var cell;
  while ((cell = document.all ? document.all[id + i++] : 
document.getElementById(id + i++))) 
    cell.style.backgroundColor = currentColor;
  if (currentColor)
    currentColor = '';
  else
    currentColor = backgroundColor;
}
</script>
</head>
<body onload="startBlink('cell', 'lightblue')">
<script type="text/javascript">
var rows = Math.floor(Math.random() * 10) + 1;
var html = '';
html += '<table border="1">'
for (var i = 0; i < rows; i++) {
  var col = Math.floor(Math.random() * rows);
  html += '<tr>';
  for (var j = 0; j < rows; j++)
    if (col == j)
      html += '<td id="cell' + i + '">Kibology<\/td>';
    else
      html += '<td>fnords<\/td>';
  html += '<\/tr>';
}
html += '<\/table>';
document.write(html);
</script>
</body>
</html>