I'm having trouble retrieving/determining the background color of a cell in an html table that I had previously colored using css. I want to be able to retrieve that color using javascript and store it into a variable for use further on in my code (not shown).
Here's a jfiddle of what I am attempting to do on a simple scale: http://jsfiddle.net/adtsolutions/6P629/. Code is also provided below:
HTML:
<body>
<table>
<tr>
<td>Hello there</td>
<td id="HDD">I want to know this cell's color</td>
</tr>
</table>
CSS:
TD {
TEXT-ALIGN: CENTER;
BORDER: 2PX SOLID BLACK;
BORDER-COLLAPSE: COLLAPSE;
BACKGROUND-COLOR: #D0A9F5;
}
JS:
//document.getElementById("HDD").style.background="#D0A9F5"
var myColor = document.getElementById("HDD").style.background;
alert(myColor);
Right now, I keep getting a "blank" when attempting to display the contents of the variable "myColor", which should be the background color. What I have noticed is that if I un-comment the first line in my javascript, myColor will clearly say what I hoped it would show. Any thoughts on what I may be doing wrong or why this only works if the background color is defined in-line in the javascript? Your constructive help is greatly appreciated!