Sunday 28 July 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...