Why I am getting "TypeError: Cannot read properties of null (reading 'addEventListener')"
Asked by Khawar Mehfooz about 2 years ago
1
<!--html-->
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="index.css">
<script src="index.js"></script>
</head>
<body>
<h1>Counter: <span id="counter">0</span></h1>
<button id="increment">Clickme</button>
</body>
<ul id="list-item"></ul>
</html>
/Js/ const incrementbtn = document.querySelector('#increment') const counterel = document.getElementById('counter') const ulelement = document.getElementById('list-item') let counter = 0 function incrementcounter() { counter++ counterel.innerText=counter const li = document.createElement('li') ulelement.appendChild(li) } incrementbtn.addEventListener('click', incrementcounter)
2 Answers
0
Getting This Error Because I included script file inside head tag. Now I moved the file in body, and it has been resolved.
0
For More information regarding this issue and its solution refer this - https://javascript.info/script-async-defer#:~:text=In%20practice%2C%20defer%20is%20used,execution%20order%20does%20not%20matter.
show more answers
Your answer