Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Uncaught TypeError: Cannot call method 'addEventListener' of null

Tags:

I'm trying to do something fairly simple, but for the reason of me probably not being good enough to search documentation, I can't get this to work.

I have a functioning inline JS that looks like this:

<A title="Wolfram IP Calc" href="javascript:txt=prompt('Enter%20IP%20address,%20e.g.%2010.20.30.40/29','1.2.3.4/5');%20if(txt)%20window.open('http://www.wolframalpha.com/input/?i='+txt);void(O);">Compute!</A> 

For various reasons, I'm trying to seperate the JS, and this is where I hit a snag.

I've created the following test page that gives me the error Uncaught TypeError: Cannot call method 'addEventListener' of null:

<HTML> <HEAD profile="http://www.w3.org/2005/10/profile"> <script type="text/javascript"> var compute = document.getElementById('compute'); compute.addEventListener('click', computeThatThing, false);  function computeThatThing() {     txt=prompt('Enter%20IP%20address,%20e.g.%2010.20.30.40/29','1.2.3.4/5');     if(txt) {         window.open('http://www.wolframalpha.com/input/?i='+txt);     } } </script></HEAD> <BODY> <A title="Wolfram IP Calc" id="compute" href="javascript:void(O);">Test</A> </BODY> </HTML> 

The only thing I've been able to find that points to a problem like that is that addEventListener can't work with <A> but should handle <IMG> (which suits me fine as I'm going to pour this on some images), so I tried adding the following to no avail:

<img id="compute" src="http://products.wolframalpha.com/images/products/products-wa.png" /> 

Thanks in advance for pointing out what I'm doing wrong. It is probably glaringly obvious, but I have close to zero experience with JS and I have gone mostly by cargo culting when I've needed it until now.

like image 305
Jan Avatar asked Mar 24 '12 22:03

Jan


People also ask

Why is addEventListener null?

The "Cannot read property addEventListener of null" error occurs when trying to call the addEventListener() method on an element that isn't present in the DOM. Variables that store a value of null are often returned from methods such as getElementById() when the element is not present in the DOM.

Why is addEventListener not working?

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).

What does uncaught TypeError Cannot read properties of null reading addEventListener ') mean?

In JavaScript, a very common error is the Uncaught TypeError Cannot read property 'addeventlistener' of null. This error occurs when JavaScript is not able to add an event listener to an element for performing a function.


1 Answers

Your code is in the <head> => runs before the elements are rendered, so document.getElementById('compute'); returns null, as MDN promise...

element = document.getElementById(id);
element is a reference to an Element object, or null if an element with the specified ID is not in the document.

MDN

Solutions:

  1. Put the scripts in the bottom of the page.
  2. Call the attach code in the load event.
  3. Use jQuery library and it's DOM ready event.

What is the jQuery ready event and why is it needed?
(why no just JavaScript's load event):

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers...
...

ready docs

like image 197
gdoron is supporting Monica Avatar answered Sep 24 '22 15:09

gdoron is supporting Monica