This is my code:
var board = document.getElementById("left");
for (var m = 0; m < 5; m++) {
var cell = document.createElement('div');
cell.className = "Cell";
cell.style.cssText='background-color:#999999;margin:5px;width:50px;height:100px;';
cell.onclick= function() {
cell.innerHTML = "Hello World";
};
cell.name = m;
board.appendChild(cell);
}
<div id="left"></div>
However, when I click every div
, "Hello World" always show on the last div
.
How can fix it?