Wednesday 11 September 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

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...