I have a function that generates random colors, I want to use this function in combination with the jQuery feature toggleClasson click. I want to create a unique color in the cell tablewhen I click and delete the generated color if the user clicks on the color cell again (make it white / colorless again).
HTML table:
<div id="container">
<table id="table">
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
</div>
Color Generation Function:
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
Color Assignment to a Cell:
$( function(){
$('td').click( function(){
$(this).css('background-color',getRandomColor);
});
});
source
share