0
"Cannot read property 'addEventListener' of null"
my code is presenting to me this error in the title the code : //gets the element vel by its id and sets it to a variable var vel = document.getElementById("vel"); vel.addEventListener("click", function() { //gets these 2 elements by thier ids var d = document.getElementById("d"); var t = document.getElementById("t"); //sets the result var var result = d / t; //get the <p> tag by its id and then puts the result into it. var x = document.getElementById("res"); x.innerHTML = result });
5 Réponses
+ 8
It's just bcz your JS gets loaded before the HTML part and so it can't find that element.
Just put your whole JS code inside a function which will be called when the window gets loaded.
You can do like this is your JS
window.onload= function (){
//Your entire JS code here
}
+ 4
It means there's no element with id "vel"
Maybe check your html
+ 3
In addition to what Flandre Scarlet said, make sure you are accessing the element after it has been created in the DOM. Something like document.onload would do the trick.
+ 3
replying to shashi ranja:
thanks a lot
+ 1
It is because the variable is not defined yet. You probably have not saved the html yet or have not defined it yet.