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

Q1463 How do I build a 10 times table in JavaScript where the numbers divisible by a dynamically changing X are highlighted?

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

Try:

<body bgcolor="#ffffff">

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

<script language="JavaScript"><!--
var Cells = 100;

function highlight(cell) {
  var color;
  for (i=1;i<=Cells;i++) {
    color = "#ffffff";
    if (i%cell==0) color = "#ff0000";
    if (document.layers)
        window.document.layers['cell' + i].bgColor = color;
    else if (document.all)
        window.document.all['cell' + i].style.background = color;
  }
}

var Text = '<table><tr>';
for (i=1;i<=Cells;i++) {
  Text += '<td class="cell" id="cell' + i + '"><a href="javascript:;" onClick="highlight(' + i + ')">' + i + '</a></td>';
  if (i%10 == 0 && i<Cells) Text += '</tr>\n<tr>';

}
Text += '</tr></table>'
document.write(Text);
//--></script>

</body>

©2018 Martin Webb