I'm trying to add a click action to the cells of an html table.
Here's my Javascript :
$(document)
.ready(
function () {
function setColor(cell) {
var cssRed = {
"color": '#ff0000'
}
var cssBlue = {
"color": '#0000ff'
}
if (cell.css('color') == '#ff0000') cell.css(cssBlue);
else cell.css(cssRed);
}
$('td').click(function () {
setColor($(this));
});
});
And here's my HTML
<table style="text-align: center" border='1'>
<tbody>
<tr>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
<tr>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
<tr>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
</tbody>
</table>
Which is supposed to change the color of the text within the clicked cell. I'm trying to make something similar to the grid in http://whenisgood.com
Although, for some reason, it keeps entering the else
statement of the if (in the setColor
method). So apparently this comparison cell.css('color') == '#ff0000'
fails every time. What did I do wrong here?
edit: I made an alert of the cells.css('color')
and here's the output :
at first rgb(11,4,0)
and then after clicking a second time : rgb(255,0,0)
edit2: At first I tried assigning the colors as blue
and red
literally. However it wouldn't work (even if I was comparing with blue
and red
). I guess Javascript won't marshall red
in Hex or RGB, right?
edit3: If you downvote, please let me know why, I'll try to update the question so it gets better.