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

156
Views
Execute part of a button only at the first time?

Without jQuery, I would like to execute a specific part of a button BUTTON only the first time one clicks it, which means the specific code won't execute after one has clicked the button, while the rest of the button's code works as usual. How do I do this?

let btn = document.getElementById("btn");
let num = document.getElementById("num");

function add(){
num.innerHTML++;
//specific code (+5 the first time one clicks the button)goes here please//
}

btn.addEventListener("click",add);
<button id="btn">BUTTON</button>
<span id="num">0</span>

<p> first click the button would add 5 to the number, then it will only add 1 each time. </p>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use a variable that remembers whenever the button has been clicked.

let btn = document.getElementById("btn");
let num = document.getElementById("num");
let btnClicked = false;

function add() {
  if (!btnClicked) {
    num.innerHTML = +num.innerHTML + 5;
    btnClicked = true
  } else {
    num.innerHTML++;
  }
}

btn.addEventListener("click", add);
<button id="btn">BUTTON</button>
<span id="num">0</span>

about 4 years ago · Juan Pablo Isaza Report
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!