I am trying to convert the text in a td cell to "Clicked!" when it is clicked upon, but it throws up an error when loading the JS. I have read around and know that it can't use an array like this but I don't know how to fix it.
window.addEventListener("load", table)
function table(){
var tables = document.getElementsByTagName("td");
tables.addEventListener("click", clicked);
}
document.getElementsByTagName
returns not a Node
object, but NodeList
object. You can access Node
objects by index.
Sample:
var tables = document.getElementsByTagName("td");
if (tables.length) {
tables[0].addEventListener("click", clicked);
}
https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByTagName
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With