Solve - addEventListener is not a function Error in JS

avatar

Borislav Hadzhiev

Wed Oct 20 20213 min read

banner

Photo by Thom Holmes

Solve - addEventListener is not a function Error #

The "addEventListener is not a function" error occurs for multiple reasons:

  • calling the method on an object that is not a valid DOM element.
  • placing the JS script tag above the code that declares the DOM elements.
  • misspelling the addEventListener method (it's case sensitive).

addeventlistener is not a function

Here is an example of how the error occurs.

index.html
<!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 this is our JavaScript code:

index.js
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 after the DOM elements. This is important, because if you run the script before having created the DOM elements, they won't be accessible in your JS code.

To solve the "addEventListener is not a function" error, make sure to call the addEventListener() method on a valid DOM element, the document or window objects and place the JS script tag at the bottom of the body tag in your HTML.

index.js
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 and the second example how to iterate over the div elements and add an event listener to each one.

If the error still persists, make sure that you haven't spelledaddEventListener wrong or got the casing wrong.

You can also console.log the element you are calling the method on.

Make sure you're calling the addEventListener method on a valid DOM element, the Document object or the Window object.

If the element you're calling the method on sometimes does not exist, you can conditionally check if the element is there before calling the addEventListener method on it.

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.

index.js
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, so for the if block to run, all of the 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 - console.log(typeof null), you will get an "object" value back, so we have to make sure the value is not null.

The last thing we check for is that the object contains the addEventListener property.

Then we know we can safely call the addEventListener method on the object.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee