Show / hide the content base with a selector switch

I am trying to switch a DIV database to a selection of radio buttons.

HTML for the switch.

<div class="row-fluid">
    <label class="radio">
      <input type="radio" name="account" id="yes" value="yes" checked>
      Yes, I have an existing account
    </label>
    <label class="radio">
      <input type="radio" name="account" id="no" value="no">
      No, I don't have an account
    </label>
</div>

DIV Content

<div id="account_contents">
    <p>This is account contents.......</p>  
</div>

This is how I tried this in jquery.

$('#no').bind('change',function(){
  $('#account_contents').fadeToggle(!$(this).is(':checked'));
  $('#account_contents').find("input").val("");
  $('#account_contents').find('select option:first').prop('selected',true);
});

But this does not work for me correctly. Here I want to show this DIV content only if the user does not have an account.

Can someone tell me how to solve this problem?

+4
source share
3 answers

it seems you need .on ('change') for the switches, not only for one of them

$('input[type="radio"][name="account"]').on('change',function(){
  var ThisIt = $(this);
  if(ThisIt.val() == "yes"){
       // when user select yes
       $('#account_contents').fadeOut();
  }else{
       // when user select no
       $('#account_contents').fadeIn();
       $('#account_contents').find("input").val("");
       $('#account_contents').find('select option:first').prop('selected',true);
  }
});

Working example

+3
source
  $(document).ready(function(){
            $('.radio input[type="radio"]').on("click", function(){
            if($('.radio input[type="radio"]:checked').val() === "yes"){
                $("#account_contents").slideDown("slow");
            }else{
                $("#account_contents").slideUp("slow");
            }
          });
     });
+2
source

, , #account_contents , :

script:

  $('#no').bind('change',function(){
  $('#account_contents').fadeToggle(!$(this).is(':checked'));
  $('#account_contents').find("input").val("");
  $('#account_contents').find('select option:first').prop('selected',true);
});
$("#yes").bind("change", function() {
  $('#account_contents').fadeOut();
});
+1

Source: https://habr.com/ru/post/1621769/


All Articles