4

I am retrieving some values from a Database and I am displaying them in a form for editing. When editing is done, the values will be sent back to the DB.

I want to show a div if a radio with value "Yes" is checked and if a radio with value "No" is checked i want to show another div.

The checked value is coming from a database so it's not always on the same radio.

<input type="radio" name="longer" id="Yes" value="Yes" <?=($info['longer']=='Yes')?'checked':''?>>Yes
<input type="radio" name="longer" id="No" value="No" <?=($info['longer']=='No')?'checked':''?>>No

<div id="first">
Show if value is yes
</div>

<div id="second">
Show if value is no
</div>

This is the jquery code I had so far but it's not working:

$(document).ready(function() {
        if ($('#No').prop('checked')) {
            $('#first').hide();
            $('#second').show();
        } else if ($('#Yes').prop('checked') {
            $('#second').hide();
            $('#first').show();
        }
});
peterh
  • 11,875
  • 18
  • 85
  • 108
b.hendriks
  • 71
  • 1
  • 7
  • Possible duplicate of [Find out if radio button is checked with JQuery?](http://stackoverflow.com/questions/2272507/find-out-if-radio-button-is-checked-with-jquery) – Chizzle Nov 24 '15 at 15:18

1 Answers1

3

check by using .is(':checked')

if ($('#No').is(':checked')) {

your code will work after document ready .. but I think you maybe need it in radio change event

 $('input[name="longer"]').on('change',function(){
       //code here
    });
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28