Reputation: 2184
I am trying to register an event handler that appends an element into the DOM when a button fires a click event,e.g.
var b = document.getElementById('evt');
var eventDemo = function(event) {
console.log('I handled the event');
console.log(event);
console.log(Object.prototype.toString.call(event));
var imgElement = document.createElement('img');
imgElement.src = 'http://lorempixel.com/150/150/';
document.body.appendChild(imgElement);
};
b.addEventListener('onclick', eventDemo, false);
but I keep getting:
Uncaught TypeError: Cannot read property 'addEventListener' of null
Why is this happening
Browser: chrome
Upvotes: 0
Views: 22896
Reputation: 307
I was getting the same kind of error and I wasted my 2 days on the same. Later found out that the same application was working fine on firefox. So I tried updating my chrome version to 53.x.x And now no such issue is occurring. Maybe this will help you too :)
Upvotes: 0
Reputation: 3819
At the point when this script runs, the element with id 'evt' is not defined. There are two possibilities:
script.js
, so it probably happens in <head>
. But when the script is loaded, the <body>
still isn't.Either add this script at the bottom of the page, or, better, use the DOMContentLoaded
event:
document.addEventListener("DOMContentLoaded", function(event) {
//place all your code here
});
And, as somebody already mentioned, the event is called click
, not onclick
. The onclick
is a DOM property, say, an equivalent in HTML to addEventListener
.
Should you ever happen to use jQuery, the convenient wrapper is $(document).ready(function() { /* place hode here */ });
Upvotes: 1
Reputation: 305
The error itself clearly explains! You don't have any html element with the id 'evt'. If you are sure you have an element with id 'evt', then use $(document).ready
as given below, so that, your js gets executed when the html elements are loaded.
$(document).ready(function(){
var b = document.getElementById('evt');
var eventDemo = function(event) {
console.log('I handled the event');
console.log(event);
console.log(Object.prototype.toString.call(event));
var imgElement = document.createElement('img');
imgElement.src = 'http://lorempixel.com/150/150/';
document.body.appendChild(imgElement);
};
b.addEventListener('onclick', eventDemo, false);
});
Upvotes: 2
Reputation: 87203
As you've said that script
is loaded in head
tag, by the time when the statement
var b = document.getElementById('evt');
is executed, there is no element in the DOM
having id evt
.
Use DOMContentLoaded
event to add event listeners on element. This will run after the DOM
is completely loaded.
The DOMContentLoaded event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. A very different event - load - should be used only to detect a fully-loaded page. It is an incredibly popular mistake for people to use load where DOMContentLoaded would be much more appropriate, so be cautious.
Code:
document.addEventListener("DOMContentLoaded", function(event) {
var b = document.getElementById('evt');
b.addEventListener('click', eventDemo, false);
});
Upvotes: 5
Reputation: 3311
From mdn
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.
In your case b is not found and returning null. Null is not an object so it cannot have addEventListener
Upvotes: 0