'Dinamically created cell does not add the event listener correctly
Am I doing it incorrectly here? According to the devtools the cells have no event listener attached.
function functionName(numRow) {
for (let i = 0; i < numRow; i++) {
let row= table.insertRow();
for (let j = 0; j < 6; j++) {
let newcell = row.insertCell();
newcell.id = "r" + i + "-c" + j;
newcell.classList.add("celdaH");
newcell.addEventListener("click", somefunction, false);
}
}
}
Stack Snippet:
const table = document.querySelector("table");
function somefunction() {
console.log("Clicked");
}
function functionName(numRow) {
for (let i = 0; i < numRow; i++) {
let row= table.insertRow();
for (let j = 0; j < 6; j++) {
let newcell = row.insertCell();
newcell.id = "r" + i + "-c" + j;
newcell.classList.add("celdaH");
newcell.addEventListener("click", somefunction, false);
}
}
}
functionName(10);
td {
width: 10px;
height: 10px;
background-color: black;
}
<table></table>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|