+ 1

How do I solve this problem in my console?

<button id = "btn"> button </div> const btn = document.getElementById("btn"); btn.addEventListener("click", function (){ // arguments }) Whenever I pass in the addEventListener to the id("btn"), there's an error in the console showing "cannot add eventListener to null"

21st Sep 2020, 9:16 AM
Ace 🤡
Ace 🤡 - avatar
2 Réponses
21st Sep 2020, 9:25 AM
Ipang
+ 1
Try this : <!DOCTYPE html> <html> <head> <title>Solution</title> </head> <body> <button id = "btn"> button </body> <script> const btn = document.getElementById("btn"); btn.addEventListener("click", function (){ // arguments }) </script> </html> Hope it help.
21st Sep 2020, 10:20 AM
AMAN GUPTA
AMAN GUPTA - avatar