Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda