When using getElementById in javascript there can only be one element per page with a given id. If there's more than one, browsers will either pick the first one, or throw an error. And, although your cascade of if statements is workable, it's less than optimal for the situation. This works:
Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function checkForm() {
var cols = document.getElementById('tableData').getElementsByTagName('td'),
colslen = cols.length, i = -1, color = checkForm.colors, val;
while(++i < colslen){
val = cols[i].innerHTML;
cols[i].style.backgroundColor = val < 5? color.red : val < 8? color.yellow : color.green;
}
}
checkForm.colors = {
green: "#7fff00",
yellow: "#ffff00",
red: "#dc143c"
}
</script>
</head>
<body>
<table id="tableData" border=1>
<tr>
<td>10</td>
<td>4</td>
<td>7</td>
</tr>
<tr>
<td>8</td>
<td>5</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>6</td>
</tr>
</table>
<script type="text/javascript">
checkForm();
</script>
</body>
</html>
Bookmarks