O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting and Setting the Selected Option in a Radio Group

if ($("#maleRB").is(":checked)){   $("#maleRB").removeAttr("checked"); else{   $("#maleRB").attr("checked"); } var genderGroup = $("input[name=gender]"); var checkedGender = genderGroup.filter(":checked"); var selectedGender = checkedGender.val();

Radio inputs are almost always used in groups. The value of a radio input that a group represents is not boolean. Instead, it is the value attribute of the currently selected element. For example, the value of the following radio button group is either "male" or "female":

<input id="maleRB" type="radio" name="gender" value="male"> <label for="maleRB">Male</label> <input id="femaleRB" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required