+ 1
What is wrong with this JavaScript code please!!
Im trying to add an event listener to a button in order to carry out a certain function but it has an error. It says that it cannot read properties of null,i dont understand. var btn=document.getElementById("button"); btn.addEventListener('click',add); Function add(){ alert("worked"); }
4 odpowiedzi
+ 3
This post might help you understand why
https://www.sololearn.com/post/90825/?ref=app
+ 6
Please link your complete code.
We can only add the event, once the html element is found – we need to "wait" for the DOM to be loaded.
There is a typo btw: "function", not "Function"
+ 3
Michael Kamau please share complete code
+ 1
Thank you Ipang,the post you shared has helped me understand something