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

155
Visualizações
jQuery active class is not removing when other button is clicked

I have two html buttons that show different information when clicked. I am using jQuery to change which button has the default button CSS and the active button CSS. My code is below. Currently, when I click the opposite button, the default active button does not switch to inactive styles. Any suggestions? Thanks in advance!

HTML:

<div class="center-buttons container space-between btn-group">
<button type="button" id="xValues" class="mobile-buttons desktop-w active-2">Button 1</button>
<button type="button" id="yValues" class="mobile-buttons desktop-w">Button 2</button></div>

jQuery:

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

CSS:

.center-buttons {
    text-align: center;
    margin: 20px 0px;
}

    .container {
    display: flex;
}

.space-between {
    justify-content: space-between;
}

.mobile-buttons {
    margin: 5px;
    border: none;
    border-bottom: 3px solid #B0B0B0;
    color: #B0B0B0;
    font-weight: 700;
    font-size: 12px;
}

.active-2, .mobile-buttons:hover {
    background-color: #fff;
    border-bottom: 3px solid #2d2d2d;
    color: #2d2d2d;
}

.desktop-w {
    width: 49%;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There seems to be a syntax error in the jQuery code. The application runs when the event handler method is terminated with "})".

enter image description here

/* The event handler method is not terminated with "})". */
$('.btn-group').on('click', 'button', function(){
  $('.btn-group button.active-2').removeClass('active-2');
  $(this).addClass('active-2');
});
.center-buttons {
  text-align: center;
  margin: 20px 0px;
}
.container {
  display: flex;
}
.space-between {
  justify-content: space-between;
}
.mobile-buttons {
  margin: 5px;
  border: none;
  border-bottom: 3px solid #B0B0B0;
  color: #B0B0B0;
  font-weight: 700;
  font-size: 12px;
}
.active-2, .mobile-buttons:hover {
  background-color: #fff;
  border-bottom: 3px solid #2d2d2d;
  color: #2d2d2d;
}
.desktop-w {
  width: 49%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="center-buttons container space-between btn-group">
<button type="button" id="xValues" class="mobile-buttons desktop-w active-2">Button 1</button>
<button type="button" id="yValues" class="mobile-buttons desktop-w">Button 2</button></div>

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