0

Iam trying to show/hide a div based on the radio button click in Yii form. But the onclick event is not working for me. Please help me to fix this as Iam a new bee to Yii. Iam using this code for radio button

 <?php echo $form->radioButtonList($model,'service_type',array(
                    '0'=>'Yes',
                    '1'=>'No',
                    'separator'=>'',
                    'onclick'=>"setVisible('Yes_box',true);setVisible('No_box', false)"));     ?>

  <div id="Yes_box" style="visibility: hidden"> contents of Yes type </div>
   <div id="No_box" style="visibility: hidden"> contents of No type </div>

This is my script:

<script>
  $(document).ready(function () {
    $("input[name$='type']").click(function () {
      var value = $(this).val();
      if (value == 'Yes') {
        $("#Yes_box").show();
        $("#No_box").hide();
      } else if (value == 'No') {
        $("#No_box").show();
        $("#Yes_box").hide();
      }
    });
    $("#Yes_box").show();
    $("#No_box").hide();
  });
</script>

When Iam using this code only the last condition is working i.e, Yes_box is showing. I think the onclick value is not passing here. Please help me to recover this problem.

anu
  • 458
  • 2
  • 13
  • 36

1 Answers1

0

I thin your code is written correctly . only few changes / may be that will work .

I have edited the your code .Check this out :

<script>
  $(document).ready(function () {
    $("input[name=type]").click(function () {
      var value = $(this).val();
      if (value == 'Yes') {
        $("#Yes_box").show();
        $("#No_box").hide();
      } else if (value == 'No') {
        $("#No_box").show();
        $("#Yes_box").hide();
      }
    });
  });
</script>

Also NOTE :- check with an alert if you are able to get data into the value variable .

Tarun Kumar
  • 508
  • 3
  • 14
  • thanks 4 the reply.... I tried the code but no result. when i run this code along with the radi button it is displaying "setVisible('Yes_box',true);setVisible('No_box', false)")" as another option – anu Feb 26 '13 at 08:04