Show/Hide div using a radio button

Asked

Viewed 6,033 times

4

Well I need to check the state of mine radio button to make sure that if he is active in "Yes", he opens the <div class="camposExtras">, click on "Not" he closes the div, and this should be accessible at all times.

Mine radio button down below:

<input type="radio" class="FlgPontua" name="FlgPontua" value="Sim" checked>
<input type="radio" class="FlgPontua" name="FlgPontua" value="Nao">

And mine div is:

<div class="camposExtras">
    Aqui vem os dados que é para esconder ou aparecer
</div>

I’m trying to do it like this:

$(".FlgPontua").change(function() {
   console.log('entro aqui')
   if ($(this).val() == "Sim") {
      $('.camposExtras').show();
   }else{
      $('.camposExtras).hide();
   }
});

But you’re not giving me anything back.

1 answer

6


You weren’t far from getting the code to work.
Instead of pointing to the class .FlgPontua, you have to point to the name="FlgPontua".

$('input[name="FlgPontua"]').change(function () {
    if ($('input[name="FlgPontua"]:checked').val() === "Sim") {
        $('.camposExtras').show();
    } else {
        $('.camposExtras').hide();
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<input type="radio" name="FlgPontua" value="Sim" checked>
<input type="radio" name="FlgPontua" value="Nao">

<div class="camposExtras">
    Aqui vem os dados que é para esconder ou aparecer
</div>

  • For some reason here it’s not working

  • It here at Stackoverflow is working correctly. You have expressed the click on the button Executar code to see it in action?

  • Yes, I forgot an important detail using a different css icheck button

  • the website is this http://icheck.fronteed.com/

  • 1

    I got, thanks for the support

Browser other questions tagged

You are not signed in. Login or sign up in order to post.