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

149
Views
Javascript / JQuery Alternar clase activa entre 2 botones en un grupo de botones

Estoy usando bootstrap con btn-group . Ahí está el html:

 <div class="btn-group btn-group-sm" role="group"> <div type="button" class="btn btn-default btn-1">Button 1</div> <div type="button" class="btn btn-default btn-2 active">Button 2</div> </div>

El botón 2 ya está activo.

Actualmente estoy haciendo esto para eliminar la clase activa de una y agregarla a otra.

 $('.btn-2').removeClass('active'); $('.btn-1').addClass('active');

¿Es esta la forma más limpia de hacer esto o hay una mejor manera para que pueda alternar esta clase activa entre los dos botones?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Usar:

 $('.btn-group').on('click', '.btn', function() { $(this).addClass('active').siblings().removeClass('active'); });

¿Creo que el código se explica por sí mismo? Simplemente elimina cualquier clase "activa" existente de los hermanos del botón en el que hizo clic y agrega esa clase al botón en el que hizo clic.

Aquí hay un fragmento de trabajo:

 $('.btn-group').on('click', '.btn', function() { $(this).addClass('active').siblings().removeClass('active'); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <div class="btn-group btn-group-sm" role="group"> <div type="button" class="btn btn-default btn-1">Button 1</div> <div type="button" class="btn btn-default btn-2 active">Button 2</div> </div>

over 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!