TypeError: Cannot read properties of null (reading 'addEventListener')
TypeError: Cannot read properties of null (reading 'addEventListener') in JavaScript means document.getElementById() or querySelector() found no element, returned null, and you called addEventListener on that null. Usually the script ran before the HTML loaded, or the id is spelled differently.
How do I fix TypeError: Cannot read properties of null (reading 'addEventListener')?
TypeError: Cannot read properties of null (reading 'addEventListener') means getElementById or querySelector found no element and returned null. Most often the script runs before the HTML has loaded, or the id is spelled differently. Add defer to your <script> tag, or move it to the end of <body>, and copy the id exactly from the HTML.
- 1. Log the lookup resultconsole.log the element right after getElementById or querySelector. If it prints null, the lookup failed.
- 2. Match the selector exactlyIds are case-sensitive. querySelector needs #save for an id and .save for a class.
- 3. Load the script laterAdd defer to a <script> in <head>, or place the tag just before </body>.
What does TypeError: Cannot read properties of null (reading 'addEventListener') mean?
getElementById and querySelector return null when nothing on the page matches. They do not throw. The crash happens on the next step, when you call .addEventListener on null. So the real question is always: why did the lookup find nothing? Safari words the same error as "null is not an object (evaluating 'btn.addEventListener')".
Why does TypeError: Cannot read properties of null (reading 'addEventListener') happen?
1. The script runs before the element exists
A <script> in the <head> runs before the browser has read the <body>, so the button is not there yet.
Breaks
<head>
<script src="app.js"></script>
</head>
<body>
<button id="save">Save</button>
</body>Works
<head>
<script src="app.js" defer></script>
</head>
<body>
<button id="save">Save</button>
</body>2. The id or class does not match
Ids are case-sensitive and must match exactly.
Breaks
<button id="saveBtn">Save</button>
document.getElementById("save-btn")
.addEventListener("click", save);Works
<button id="saveBtn">Save</button>
document.getElementById("saveBtn")
.addEventListener("click", save);3. Missing # or . in querySelector
querySelector takes a CSS selector. "save" looks for a <save> tag, not id="save".
Breaks
document.querySelector("save")
.addEventListener("click", save);Works
document.querySelector("#save")
.addEventListener("click", save);4. The same script runs on a page without that element
One app.js shared by several pages crashes on the page that has no such button. Check before using it.
Breaks
const btn = document.getElementById("save");
btn.addEventListener("click", save);Works
const btn = document.getElementById("save");
if (btn) {
btn.addEventListener("click", save);
}How to find it in your own code
Checklist: 1) console.log the element right after you look it up; it will print null. 2) Copy the id from the HTML and paste it into the JavaScript to rule out typos. 3) Check querySelector has # for ids and . for classes. 4) Add defer to the <script> tag, or move the tag to the end of <body>. 5) If the element is created later by your code, look it up after creating it.
Practise null and undefined errors
None of the challenges raises this exact error yet. These practise the same kind of bug.
Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.