Home:ALL Converter>radio button hide and show div jquery

radio button hide and show div jquery

Ask Time:2011-11-14T20:28:07         Author:user494766

Json Formatter

I have a radio button with two possible values yes or no. I would like when user press yes a div will be appeared and when no hide the div.

My code is but it does not work:

<script type="text/javascript">
                   $(document).ready(function() {
                   $("div.desc").hide();
                   $("input[name$='pain']").click(function() {
                   var test = $(this).val();
                   $("div.desc").hide();
                  $("#" + test).show();
                    });
                  });
               </script>

         <div id="fieldsetInner">
               <fieldset class="rightInner">
               <legend>pain</legend>
               <label for="pain" class="label2">do you feel pain ; </label>
               <input name="pain" type="radio" value="yes" id="yes" checked="checked"/> Yes  &nbsp;
               <input name="pain" type="radio" value="no"/> No

               <div id="yes" class="desc">
                <label for="pain-feature1-morph" class="label2">Morph : </label>
               </div>
          </div>

Thanks in advance

Author:user494766,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/8121577/radio-button-hide-and-show-div-jquery
Mr.T.K :

I hope this will help you\n\n <input name=\"pain\" type=\"radio\" value=\"yes\" id=\"yes\"/> Yes\n <input name=\"pain\" type=\"radio\" value=\"no\" id=\"no\" /> No\n\n\n $('input:radio[name=pain]').click(function() {\n if ($('#yes').attr('checked')) {\n $('.desc').show();\n } else if ($('#no').attr('checked')) {\n $('.desc').hide();\n }\n\n });\n",
2011-11-14T12:42:43
Manuel van Rijn :

you could do\n\n$(\"input#yes\").click(function() {\n $(\"div.desc\").show();\n});\n$(\"input#no\").click(function() {\n $(\"div.desc\").show();\n});\n",
2011-11-14T12:31:56
yy