Tried to change the color of a cell by clicking on it.
The cells are normaly grey and by the first click they should turn red. When I click on a red cell it should turn grey again.
function changeColor(cell) {
var red = '#FE2E2E';
var grey = '#E6E6E6';
if (cell.style.backgroundColor == grey) {
cell.style.backgroundColor = red;
} else {
if (cell.style.backgroundColor == red) {
cell.style.backgroundColor = grey;
}
};
};
#table tr td {
width: 20px;
height: 50px;
cursor: pointer;
background-color: #E6E6E6;
border: 1px solid black;
}
<table class="table table-bordered" id="table">
<tbody>
<tr>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
</tr>
</tbody>
</table>
I also tried with .style.bgColor
, rgb
and if (cell.style.backgroundColor ===
but this didn't work either. The value of the cells background color is either by .backgroundColor : '' or by .bgColor : undefined.