Monday, 9 September 2013

Use Javascript to change a table cell color if its value is less than another

Use Javascript to change a table cell color if its value is less than another

Cant figure out what I'm doing wrong here:
Just want cell .tt to be red when its numerical values is less than cell dd
HTML:
<table class="colorMe">
<tr><td class="tt">2000</td><td>3500</td></tr>
<tr><td>3000</td><td>2500</td></tr>
<tr><td id="dd">4000</td><td>4500</td></tr>
</table>
JS:
$(".colorMe .tt" ).each(function() {
var val = parseInt(this.innerHTML, 10);
if (val < document.getElementById("dd");) {
this.style.backgroundColor = "#F00000";
}
});
No Idea why this isn't working

No comments:

Post a Comment