Home > Article > Web Front-end > TypeError document.getElementById(...) is null error reason_javascript skills
Today I hand-wrote a simple page for testing. After writing the js part, I always reported TypeError document.getElementById(...) is null. I read the code several times and found no syntax errors. No. The solution was to search on the Internet and found that the syntax of document.getElementById for finding nodes based on ID is a bit particular. The details are as follows:
(1) If it is written in front of the node where the id is located, such as in the head block, it should be written as follows:
(2) Write it after the node where the id is located, otherwise it will report TypeError: document.getElementById(...) is null, which cannot find the node.
If you write too much jquery code, it is easy to overlook some js details, so record them for future reference.