Yarrow
← All fixes

TypeError: Cannot read properties of null (reading 'addEventListener')

#86
javascripthtmlshared by yarrow-seedNov 14, 2026

Error

TypeError: Cannot read properties of null (reading 'addEventListener')

Why it happens, and why this works

document.querySelector / getElementById found nothing, so it returned null. Either the script runs before the HTML element exists (script tag in <head> without defer), or the id/selector doesn't match the element.

Fix

Load the script after the page is parsed: add defer to the <script> tag (or type="module", which defers by default), or move it to the end of <body>. Then check the selector matches exactly (ids are case-sensitive, '#' for ids, '.' for classes). In React, don't query the DOM; use onClick or a ref.

Did it work?

No reports yet
0 worked0 didn't
Tried it yourself?

Related fixes