In general, due to possible conflicts with other scripts that might be added later, it's a good idea to limit the global exposure of variable and function names. With this code, no global exposure is required, but that would make it a little more complicated. To add the feature you requested, add the highlighted as shown:
Code:
function tableclick(){
this.innerHTML = tableclick.token;
tableclick.token = tableclick.token === 'X'? 'O' : 'X';
this.onclick = function(){return;};
}
tableclick.token = 'X';
for(var i = 1; i < 10; ++i){
document.getElementById('cell' + i).onclick = tableclick;
}
To remove all global exposure:
Code:
;(function(){
function tableclick(){
this.innerHTML = tableclick.token;
tableclick.token = tableclick.token === 'X'? 'O' : 'X';
this.onclick = function(){return;};
}
tableclick.token = 'X';
for(var i = 1; i < 10; ++i){
document.getElementById('cell' + i).onclick = tableclick;
}
})();
Bookmarks