Last updated: Mar 3, 2024
Reading timeยท3 min
The "TypeError: addEventListener is not a function" error occurs for multiple reasons:
addEventListener
method (it's case-sensitive).Here is an example of how the error occurs.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div class="box">Box 1</div> <div class="box">Box 2</div> <div class="box">Box 3</div> <!-- โ Your JS script here โ --> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const boxes = document.getElementsByClassName('box'); console.log(boxes); // ๐๏ธ [div.box, div.box, div.box] // โ๏ธ TypeError: boxes.addEventListener is not a function boxes.addEventListener('click', function onClick() { console.log('box clicked'); });
We got the error because we called the
addEventListener
method on the array-like object that the getElementsByClassName
method
returns.
Notice that in our HTML
file, we placed the index.js
script at the bottom of
the body
tag, after the DOM elements.
To solve the "addEventListener is not a function" error, make sure to call the
addEventListener()
method on a valid DOM element, or the document
or
window
objects, and place the JS script tag at the bottom of the body
tag in
your HTML.
const boxes = document.getElementsByClassName('box'); console.log(boxes); // ๐๏ธ [div.box, div.box, div.box] // โ addEventListener to first box boxes[0].addEventListener('click', function onClick() { console.log('box clicked'); }); // โ addEventListener to all boxes for (const box of boxes) { box.addEventListener('click', function onClick() { console.log('box clicked'); }); }
The first example calls the addEventListener
method on the first element in
the array-like object.
The second example shows how to iterate over the div
elements to add an event
listener to each.
addEventListener
wrong.You can also console.log
the element you are calling the method on.
addEventListener
method on a valid DOM element, or on the Document
or Window
objects.addEventListener
If the element you're calling the method on sometimes doesn't exist,
conditionally
check if the element is there
before calling the addEventListener
method.
For example, a basic DOM element has a type of object, so we can check if the
value is an object and contains the addEventListener
property before calling
the method.
const box = null; if ( typeof box === 'object' && box !== null && 'addEventListener' in box ) { box.addEventListener('click', function onClick() { console.log('box clicked'); }); }
Our if
condition uses the logical AND (&&) operator. For the if
block to
run, all conditions have to be met.
We first check if the box
variable
stores a value with a type of object
,
because DOM elements have a type of object
.
Then we
check if the variable is not equal to null.
Unfortunately, if you check the type of null
with console.log(typeof null)
,
you would get an "object"
value back, so we have to make sure the value is not
null
.
console.log(typeof null); // ๐๏ธ object
addEventListener
property.Then we know that we can safely call the addEventListener
method on the
object.
You can learn more about the related topics by checking out the following tutorials: