Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda