I am trying to increment when increment button is pressed but I am getting [object HTMLParagraphElement]1

I am working on this increment function when the increment button is pressed that should add +1 to count element.
This is my html:

    <p id="count-el">0</p>
    <button id="increment-btn" onclick="increment()">INCREMENT</button>

JS:


let countEl = document.getElementById('count-el');

function increment() {
    countEl.textContent = countEl + 1;
}

>Solution :

You need to create a variable and then increment that variable in your increment function.

let count = 0;
let countEl = document.getElementById('count-el');
countEl.textContent = count;

function increment() {
  count++;
  countEl.textContent = count;
}

Leave a Reply