I have a simple task to create a counter so that I can subtract and add a number by pressing, but it gives an error in the console.
This is an error.
index.html:21 Uncaught TypeError: Cannot set properties of null (setting 'onclick')
at index.html:21:42
(anonymous) @ index.html:21
This is a code.
<!DOCTYPE html>
<html>
<head>
<title>Counter</title>
</head>
<style>
p{
font-size: 2rem;
}
button{
font-size: 2rem;
}
</style>
<script>
let integer = 0;
document.getElementById('minus').onclick = function(){
integer-=1;
document.getElementsByClassName('number').innerHTML = integer;
};
document.getElementById('plus').onclick = function(){
integer+=1;
document.getElementsByClassName('number').innerHTML = integer;
};
</script>
<body>
<p class="number">0</p>
<button id="minus">-</button>
<button id="plus">+</button>
</body>
</html>
I tried many ways but nothing helps.
`
– NNL993 Dec 30 '22 at 13:43