HTML Code:
0
HTML Code:
Ts code:
var buttonInc = document.getElementById("increment");
buttonInc?.addEventListener("click",IncrementVal);
const counterDiv = document.getElementById("counterval");
var count=0;
function IncrementVal()
{
count++;
console.log("Count is : " + count)
updateCounter();
}
function updateCounter() {
if(counterDiv)
counterDiv.textContent = count.toString();
console.log(counterDiv)
}
Why value is not updating on click
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sreenath KappoorPosted Jun 9, 2025, 11:54 AM
you're using both onclick in HTML and addEventListener in TypeScript for the same button.
Update your HTML to remove onclick
update your vm
window.addEventListener("DOMContentLoaded", () => {
const buttonInc = document.getElementById("increment");
const counterDiv = document.getElementById("counterval");
let count = 0;
function updateCounter() {
if (counterDiv) {
counterDiv.textContent = count.toString();
}
}
function IncrementVal() {
count++;
console.log("Count is:", count);
updateCounter();
}
buttonInc?.addEventListener("click", IncrementVal);
});