PDA

View Full Version : use javascript to get the bgcolor of html table cell



zpzpzp
January 7th, 2009, 11:08 PM
Hi

An html table cell is defined as follows:
<td rowspan="2" bgcolor="red" width="10"></td>

Does anyone know how to use javascript to get the bgcolor of this table cell?

I tried this following code, but in the popup window there is nothing displayed (not "undefined".)


var table = document.getElementById("automationTestResults");
var tableBody = table.getElementsByTagName("tbody")[0];
var rows = tableBody.getElementsByTagName("tr");
var cellsInARow = rows[1].getElementsByTagName("td");

alert (cellsInARow[3].style.backgroundColor);

Thanks,

Paul

myrtle1908
January 8th, 2009, 12:03 AM
bgcolor is an attribute NOT a style property ...


<table>
<tr><td bgcolor="red" id="blah">Blah</td></tr>
</table>

<script>
var td = document.getElementById("blah");
alert(td.bgColor);
</script>