Wednesday, September 11, 2019

javascript - document.getElementById() doesn't work?




No much to say,











The alert message is null instead of a message contain details of the object or something like that.



What is the problem?


Answer



Put your script AFTER the actual element, otherwise by the time your javascript code runs, there's not yet such element in the DOM.











Alternatively put your script in a DOM ready event to ensure that the DOM is fully loaded by the browser before attempting to manipulate it:










No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...