I'm trying to set a global event handler for an image, but running into issues.
When I use the code
document.getElementById("post_image").onclick = photoEnlarge;
, it returns an error saying Uncaught TypeError: Cannot set property 'onclick' of null. This is because the image with the ID post_image
is being loaded via window.onload
and createElement("img")
How can I fix this issue and make the above code work without any errors?
Additionally, I have a question regarding the proper use of global event handlers. Is it correct to place them directly before the closing body tag along with other JavaScript?