JavaScript does not detect clicks on table cells, code does not work. Code:
document.querySelectorAll("td").forEach(e => e.addEventListener("click", function(){
console.log(this.cellIndex);
console.log(this.innerText);
console.log(document.querySelectorAll("th")[this.cellIndex].innerText);
}));
If I paste this code into the console it works, but as a separate .js file or in html code it does not work. What could be wrong?