How To: jQuery - Get and Set a Radiobutton Value

To get the radiobutton value, select the radiobutton elements using the name and the jQuery ":checked" selector. To set the value of a radiobutton, use the jQuery prop method for jQuery after version 1.6 or the attr method for jQuery version 1.6 or below.

Syntax

HTML for the following Code

<input name="pet" type="radio" value="dog" /><span>Dog</span>
<input name="pet" type="radio" value="cat" /><span>Cat</span>
<input name="pet" type="radio" value="fish" /><span>Fish</span>

Get Selected Radiobutton Value

var pet = $("input[name='pet']:checked").val();

Set Selected Radiobutton Value (jQuery version > 1.6)

Set the "Dog" radiobutton value to checked.

$("input[value='dog']").prop("checked", true);

Set Selected Radiobutton Value (jQuery version <= 1.6)

Set the "Cat" radiobutton value to checked.

$("input[value='cat']").attr("checked", true);

Example

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License