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

117
Views
cómo activar una nueva función después de hacer clic en el botón tres veces

quiero agregar una nueva función después de hacer clic en el botón 3 veces y borrar/eliminar la función anterior

archivo HTML:

 <body> <div id="background"> <p>this background white, before</p> </div> <button class="btn-1">change color</button> </body>

JavaScript:

 const btn1 = document.querySelector(".btn-1") ; const bg = document.getElementById("background") const toRed = ()=>{ bg.style.backgroundColor = "red"; } const toBlue = ()=>{ bg.style.backgroundColor = "steelblue"; } btn1.addEventListener('click', toRed);

// quiero que este btn1 tenga una función para borrar la función de rojo y agregar a azul en su lugar después de hacer clic 3 veces

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

0

elimine el detector de eventos y agregue uno nuevo cuando se hizo clic tres veces:

 const btn1 = document.querySelector(".btn-1"); const bg = document.getElementById("background"); var redClicked = 0; const toRed = ()=>{ bg.style.backgroundColor = "red"; redClicked++; if (redClicked >= 3) { btn1.removeEventListener('click', toRed); btn1.addEventListener('click', toBlue); } } const toBlue = ()=>{ bg.style.backgroundColor = "steelblue"; } btn1.addEventListener('click', toRed);
 <body> <div id="background"> <p>this background white, before</p> </div> <button class="btn-1">change color</button> </body>

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!