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

221
Views
cómo cambiar la clase al hacer clic en el botón en javascript

Tengo este script, pero necesito cambiar la clase a "ir" o "detener". actualmente puedo activar ambos para cambiar a la clase pero debería cambiar a uno u otro

Código

 var form = document.querySelector("form"); form.addEventListener("click", function(event) { event.preventDefault(); const classId = event.target.id; if (classId == "go") { event.target.className = "go"; } else if (classId == "stop") { event.target.className = "stop"; } });
 .go { background: green; color: white; } .stop { background: red; color: white; }
 <form action=""> <button id="go">GO!</button> <button id="stop">STOP!</button> </form>

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Está agregando la clase al botón en el que se hizo clic, pero nunca eliminando la clase del otro botón.

 var form = document.querySelector("form"); const goButton = document.querySelector("#go"); const stopButton = document.querySelector("#stop"); form.addEventListener("click", function(event) { event.preventDefault(); if (event.target == goButton) { goButton.classList.add("go"); stopButton.classList.remove("stop"); } else if (event.target == stopButton) { stopButton.classList.add("stop"); goButton.classList.remove("go"); } });
 .go { background: green; color: white; } .stop { background: red; color: white; }
 <form action=""> <button id="go">GO!</button> <button id="stop">STOP!</button> </form>

about 4 years ago · Santiago Trujillo 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!