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

152
Visualizações
Change background of a card when radio selected

My question is very simple : how to add a background color to my card element when the button radio is selected ?

<div class="col-6 col-md-3 mt-4 text-center my-auto">
            <label for="'.$abreviation.'">
                <div class="card card-block d-flex">
                    <div class="card-body align-items-center d-flex justify-content-center">
                        <input type="radio" id="'.$abreviation.'" name="tri" value="'.$abreviation.'" class="check-on" /> '.$tri.'
                    </div>
                </div>
            </label>
        </div>

Is there any way to achieve this with just css or I have to use javascript or jquery ? I tried to do this but all of the cards are in a red backgroundColor instead of just the card where the radio is selected.


    $(document).ready(function(){

        $(".check-on").click(function(){

          $('.check-on').parent().parent().addClass('backgroundCard') 

        });

    });

Thanks a lot for your help.

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

0

  1. You don't need to bind a label to an input with the for attribute when the input tag is inside a label tag.
  2. The card can be styled without any JS. To do this, you can place a card next to the input field, hide the input using the d-none class, and use the adjacent element combinator.

Try to click the cards. They work as radio buttons now.

input[type="radio"]:checked + .card {
  background: #f1f1f1;
}
<div class="col-6 col-md-3 mt-4 text-center my-auto">
    <label>
        <input type="radio" name="tri" value="'.$abreviation.'" class="d-none" />
        <div class="card card-block d-flex">
            <div class="card-body align-items-center d-flex justify-content-center">
                '.$tri.'
            </div>
        </div>
    </label>
</div>

<div class="col-6 col-md-3 mt-4 text-center my-auto">
    <label>
        <input type="radio" name="tri" value="'.$abreviation2.'" class="d-none" /> 
        <div class="card card-block d-flex">
            <div class="card-body align-items-center d-flex justify-content-center">
                '.$tri.'
            </div>
        </div>
    </label>
</div>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

about 4 years ago · Juan Pablo Isaza Relatório

0

$(document).ready(function(){
    $(".check-on").click(function(){
          $(this).parent().parent().addClass('backgroundCard')
    });
});

once you have focused on the clicked element, keep focus on that only using this selector. You where again selecting all by class

about 4 years ago · Juan Pablo Isaza Relatório

0

You can add and remove class with on change event.

In case you have multiple radio groups, you can get the name of those radio and based on this you can remove the class of the correct cards:

$(this).attr('name');

$('.check-on').on('change', function() {
  let radioName = $(this).attr('name');
  $(`.check-on[name=${radioName}]`).closest('.card.backgroundCard').removeClass('backgroundCard');
  $(this).closest('.card').addClass('backgroundCard');
});
.card.backgroundCard {
  background: #f1f1f1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<div class="col-6 col-md-3 mt-4 text-center my-auto">
    <label for="'.$abreviation.'">
        <div class="card card-block d-flex">
            <div class="card-body align-items-center d-flex justify-content-center">
                <input type="radio" id="'.$abreviation.'" name="tri" value="'.$abreviation.'" class="check-on" /> '.$tri.'
            </div>
        </div>
    </label>
</div>

<div class="col-6 col-md-3 mt-4 text-center my-auto">
    <label for="'.$abreviation2.'">
        <div class="card card-block d-flex">
            <div class="card-body align-items-center d-flex justify-content-center">
                <input type="radio" id="'.$abreviation2.'" name="tri" value="'.$abreviation2.'" class="check-on" /> '.$tri.'
            </div>
        </div>
    </label>
</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