I’m trying to build a javascript counter, but my code bugged:
let counterDisplayElem = document.querySelector('.counter-display');
let counterMinusElem = document.querySelector('.counter-minus');
let counterPlusElem = document.querySelector('.counter-plus');
let count = 0;
updateDisplay();
counterPlusElem.addEventListener("click", () => {
count++;
updateDisplay();
});
counterMinusElem.addEventListener("click", () => {
count--;
updateDisplay();
});
function updateDisplay() {
counterDisplayElem.innerHTML = count;
}
<h1 class="counterdisplay">(..)</h1>
<button class="counter-minus">-</button>
<button class="counter-plus">+</button>
I looked online but I didn’t find an answer…
2
Answers
The class name doesn’t match between on h1 tag and your js code.
Fix:
Change
to
Your classes name don’t match:
You should have this:
But instead of this, so change it:
And also, even though it won’t affect your js code in this case, it is ALWAYS a good practice to put a semi-colon
;
at the end of every js line/functionHope this helps!