Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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;
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading