Sunday, July 28, 2019

javascript - "getElementsByTagName(...)[0]" is undefined?



I have the following code, which basically toggles through a bunch of images.











Press Here!







For some reason, when I run it, nothing happens, because of the following error as displayed by my Firebug console.



    hbutton is undefined    

---
hbutton.onclick = function() {


When I run just the JS after the page has loaded however, it works perfectly fine!!! Why is this?


Answer



Your code is executing before the h1 tag is defined. You must run it in an onload handler or put it just before /body


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...