I'm coding a tictactoe game but the code which I'm using to check whether the user is clicking on an empty square or already filled is not working for me please see what mistake I am doing
function startgame(){
var $board=$('#board');
$('div.square').remove();
for(var i=0;i<9;i++)
$board.append($('<div/>').addClass('square').addClass('empty'));
$('div.square.empty').click(function(){
$this=$(this);
if($('div.square.empty').length==0){
displayendmsg();
}
else {
$this.removeClass('empty');
if(currentplayer=="X")
$this.append($('<div><img src="cross.jpg"> </div>').addClass('cross').css('visibility','visible'));
else
$this.append($('<div><img src="circle.jpg"> </div>').addClass('circle').css('visibility','visible'));
flipturn();
}
});
};
Even on clicking already occupied square I'm enterin the handler don know why ?