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

102
Visualizações
Default radio button option with jquery

I am trying to hide/show different divs using the method shown here.

<style>
    .box{
        color: #fff;
        padding: 20px;
        display: none;
        margin-top: 20px;
    }
    .red{ background: #ff0000; }
    .green{ background: #228B22; }
    .blue{ background: #0000ff; }
    label{ margin-right: 15px; }
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $('input[type="radio"]').click(function(){
        var inputValue = $(this).attr("value");
        var targetBox = $("." + inputValue);
        $(".box").not(targetBox).hide();
        $(targetBox).show();
    });
});
</script>
</head>
<body>
    <div>
        <label><input type="radio" name="colorRadio" value="red" checked = "checked"> red</label>
        <label><input type="radio" name="colorRadio" value="green"> green</label>
        <label><input type="radio" name="colorRadio" value="blue"> blue</label>
    </div>
    <div class="red box">You have selected <strong>red radio button</strong> so i am here</div>
    <div class="green box">You have selected <strong>green radio button</strong> so i am here</div>
    <div class="blue box">You have selected <strong>blue radio button</strong> so i am here</div>
</body>

I would like to have the red button clicked by default when the page loads. However if I just change the label to this:

<label><input type="radio" name="colorRadio" value="red" checked = "checked"> red</label>

It shows as clicked:

checked but not showing

but I have to click it again to actually load the red div:

shows after clicking

Is there an elegant way to fix that so it is showing the red div when the page loads?

Thanks a lot, Alex

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

0

How about just adding the $('.red').show() to set which box should be shown as default?

$(document).ready(function () {
  $('input[type="radio"]').click(function () {
    var inputValue = $(this).attr('value');
    var targetBox = $('.' + inputValue);
    $('.box').not(targetBox).hide();
    $(targetBox).show();
  });

  $('.red').show();
});
.box {
  color: #fff;
  padding: 20px;
  display: none;
  margin-top: 20px;
}
.red {
  background: #ff0000;
}
.green {
  background: #228b22;
}
.blue {
  background: #0000ff;
}
label {
  margin-right: 15px;
}
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

<body>
    <div>
        <label><input type="radio" name="colorRadio" value="red" checked> red</label>
        <label><input type="radio" name="colorRadio" value="green"> green</label>
        <label><input type="radio" name="colorRadio" value="blue"> blue</label>
    </div>
    <div class="red box">You have selected <strong>red radio button</strong> so i am here</div>
    <div class="green box">You have selected <strong>green radio button</strong> so i am here</div>
    <div class="blue box">You have selected <strong>blue radio button</strong> so i am here</div>
</body>

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