Amidude
Amidude

Reputation: 351

Reset radio to unchecked when selecting another radio button

Using older version of jQuery (1.3)

I have a form like so:

    <form id="myForm">
     <input type="radio" id="yes" value="yes" /><label for="yes">Yes</label><br />
     <input type="radio" id="no" value="no" /><label for="no">No</label>
    </form>

My jQuery is as follows:

    $('#myForm').change(function() {
    if($('#yes').attr('checked')) {
        $('#lookUp').show();
    }
    else if($('#no').attr('checked')) {
        $('#lookUp').hide();
        $('#yes').attr('checked', false);
    }
});

I'm showing a div when the user clicks on "yes" and hiding it when "no" is selected. I would also like to set the checked status of "yes" to false but this is not working. Any suggestions would be greatly appreciated.

Upvotes: 1

Views: 531

Answers (1)

Ryan Beaulieu
Ryan Beaulieu

Reputation: 453

Set the Input name attributes to be the same so the form knows they are the same HTML radio button group

 <input type="radio" id="yes" name="whatever" value="yes" /><label for="yes">Yes</label><br />
 <input type="radio" id="no" name="whatever" value="no" /><label for="no">No</label>

Upvotes: 3

Related Questions