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

212
Visualizações
Only give active class when clicking the button?

So with this code when I click on the card the card becomes selected. I would like only the button to be able to assign the active class. Thanks In advance, I'm still learning how to properly use Javascript!!

$(".service-option-card").click(function() {
  $(this).addClass("active").siblings('.active').removeClass('active');
});
.service-option-container {
  margin: 1em 0 4em 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  column-gap: 1em;
  row-gap: 1em;
}

.service-option-container .service-option-card {
  border: 1px solid black;
  border-radius: 20px;
  padding: 1em;
  margin-left: 1em;
  margin-right: 1em;
}

.service-option-container .service-option-card .service-option-btn {
  margin: 1em 0;
}

.service-option-container .service-option-card .extra-pad-bottom {
  padding-bottom: 2em;
}

.service-option-container .service-option-card .option-price {
  font-weight: bold;
}

.service-option-container .service-option-card:hover {
  cursor: pointer;
}

.service-option-container .service-option-card.active {
  background-color: #efeeee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="service-option-container">
  <div class="service-option-card">Card Content goes in here <br><a class="service-option-btn">Button</a></div>
  <div class="service-option-card">Card Content goes in here <br><a class="service-option-btn">Button</a></div>
  <div class="service-option-card">Card Content goes in here <br><a class="service-option-btn">Button</a></div>
  <div class="service-option-card">Card Content goes in here <br><a class="service-option-btn">Button</a></div>
  <div class="service-option-card">Card Content goes in here <br><a class="service-option-btn">Button</a></div>
  <div class="service-option-card">Card Content goes in here <br><a class="service-option-btn">Button</a></div>
  <div class="service-option-card">Card Content goes in here <br><a class="service-option-btn">Button</a></div>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Change the JS selector to point to the button and then the $(this) needs to look up to the parent element using .parent() to apply the active class to the card

Also added css to the button to make it easier to see!

$(".service-option-btn").click(function() {
  $(this).parent().addClass("active").siblings('.active').removeClass('active');
});
.service-option-container {
  margin: 1em 0 4em 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  column-gap: 1em;
  row-gap: 1em;
}

.service-option-container .service-option-card {
  border: 1px solid black;
  border-radius: 20px;
  padding: 1em;
  margin-left: 1em;
  margin-right: 1em;
}

.service-option-container .service-option-card .service-option-btn {
  margin: 1em 0;
}

.service-option-container .service-option-card .extra-pad-bottom {
  padding-bottom: 2em;
}

.service-option-container .service-option-card .option-price {
  font-weight: bold;
}

.service-option-container .service-option-card:hover {
  cursor: pointer;
}

.service-option-container .service-option-card.active {
  background-color: #efeeee;
}

.service-option-btn {
  background-color: red;
  padding: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="service-option-container">
  <div class="service-option-card">Card Content goes in here <br><a class="service-option-btn">Button</a></div>
  <div class="service-option-card">Card Content goes in here <br><a class="service-option-btn">Button</a></div>
  <div class="service-option-card">Card Content goes in here <br><a class="service-option-btn">Button</a></div>
  <div class="service-option-card">Card Content goes in here <br><a class="service-option-btn">Button</a></div>
  <div class="service-option-card">Card Content goes in here <br><a class="service-option-btn">Button</a></div>
  <div class="service-option-card">Card Content goes in here <br><a class="service-option-btn">Button</a></div>
  <div class="service-option-card">Card Content goes in here <br><a class="service-option-btn">Button</a></div>
</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