In the process of creating a fun game where two players take turns drawing on a grid by clicking <td>
elements. Currently, I have implemented the functionality to toggle the background color of a <td>
element from black to white and vice versa with consecutive clicks. However, I'm facing an issue where after the first click, the third click doesn't trigger as expected.
$("td").click(function(){
alert(0);
alert($(this).css('background-color')); // alerts rgb(0, 0, 0)
if($(this).css('background-color') == 'rgb(255, 255, 255)' || 'rgba(0,0,0,0)'){
$(this).css('background-color', 'black');
alert(1);
var color = 'black';
} else{
alert(2);
$(this).css('background-color', 'white');
var color = 'white';
}
//alert(event.target.id);
update( event.target.id, color);
});
The data reading function is working flawlessly
function read(){
$.post("ajax.php",
{
read: '1',
},
function(data){
values=data.split('*');
//alert(values[1]);
var id = '#' + values[0]+ '';
//alert(id);
$( id ).css('background-color', values[1]);
}
);
}
I have included an HTML table for the game's grid structure, generated dynamically through PHP. It's currently set as a 5x5 grid for simplicity.
<table>
<tr>
<td style="height: 21px; width: 21px; background-color:white;" id="a1"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="a2"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="a3"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="a4"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="a5"></td>
</tr>
<tr>
<td style="height: 21px; width: 21px; background-color:white;" id="b1"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="b2"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="b3"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="b4"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="b5"></td>
</tr>
<tr>
<td style="height: 21px; width: 21px; background-color:white;" id="c1"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="c2"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="c3"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="c4"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="c5"></td>
</tr>
<tr>
<td style="height: 21px; width: 21px; background-color:white;" id="d1"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="d2"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="d3"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="d4"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="d5"></td>
</tr>
<tr>
<td style="height: 21px; width: 21px; background-color:white;" id="e1"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="e2"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="e3"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="e4"></td>
<td style="height: 21px; width: 21px; background-color:white;" id="e5"></td>
</tr>
</table>
Would greatly appreciate any assistance provided. Thank you