Salve vi posto il codice js:
Codice PHP:
function GameBoard(){
this.initTable = function(row, col, tableWidth, tableHeight){
//var text = document.createTextNode(" ");
var table = document.getElementById("scacchiera");
var idCounter = 0;
var tableDate = null;
var tableRow = null;
for(righe = 0; righe < row; righe++){
tableRow = document.createElement("tr");
for(colonne = 0; colonne < col; colonne++){
tableDate = document.createElement("td");
tableDate.y= colonne;
tableDate.x=righe;
tableDate.setAttribute("id", idCounter);
//---------da css-------------
tableDate.setAttribute("width", "80");
tableDate.setAttribute("height", "80");
//----------------------------
tableDate.onclick = function() {gameBoard.setPedina(idCounter)};
tableRow.appendChild(tableDate);
idCounter++
}
tableRow.setAttribute("bgcolor", "red");//css
table.appendChild(tableRow);
}
table.setAttribute("border", "1");//css
table.style.width=tableWidth;//css
table.style.height=tableHeight;//css
}
this.setPedina = function(id) {
var pedinaRossa = new Image();
pedinaRossa.src="pedina.jpeg";
//var id = .getAttribute("id");
var tableDate = document.getElementById(id);
alert(id);
var img = document.createElement("img");
img.src = pedinaRossa.src;
img.width=80;//css
img.height=80;//css
img.setAttribute("border", "1");//css
tableDate.appendChild(img);
}
this.isAlive = function() {
alert("isAlive");
}
}
allora, creo in modo dinamico una tabella 8x8 caselle e assegno ad ogni tag "td" un id con idCounter per poi chiamare, all'evento onclick setPedina con l'id relativo della cella.
Il problema è che su qualsiasi cella clicchi mi da id = 64!
Ovvio che mi sfugge qcs...