+ 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"
2 Answers
+ 6
Incomplete DOM
https://www.sololearn.com/post/90825/?ref=app
+ 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.