Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
buttons work the opposite after first click

I'm trying to build a beginner app that will increment and decrement a given number on a mouse event. However, the buttons act weird. Each button increments and decrement but only after the second click. After the first they do the opposite, ie. the increment button = decrements and vice versa. Can anyone tell why it happening? (The buttons work weird after the first click then work just fine)

let batch = document.getElementById("count-el");
let batchConverted = Number(batch.innerText);
const incrementBtn = document.getElementById("increment-btn");
const decrementBtn = document.getElementById("decrement-btn");

incrementBtn.addEventListener("click", increment);
decrementBtn.addEventListener("click", decrement);

function increment() {
  let batchIncrement = batchConverted++;
  batch.innerText = batchIncrement;
}

function decrement() {
  let batchDecrement = batchConverted--;
  batch.innerText = batchDecrement;
}
<h1>People entered:</h1>
<h2 id="count-el">0</h2>
<button id="increment-btn">Increment</button>
<button id="decrement-btn">Decrement</button>

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!