HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptCritical

How to check a radio button with jQuery?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
withhowbuttoncheckjqueryradio

Problem

I try to check a radio button with jQuery. Here's my code:









And the JavaScript:

jQuery("#radio_1").attr('checked', true);


Doesn't work:

jQuery("input[value='1']").attr('checked', true);


Doesn't work:

jQuery('input:radio[name="type"]').filter('[value="1"]').attr('checked', true);


Doesn't work:

Do you have another idea? What am I missing?

Solution

For versions of jQuery equal or above (>=) 1.6, use:

$("#radio_1").prop("checked", true);


For versions prior to (<) 1.6, use:

$("#radio_1").attr('checked', 'checked');


Tip: You may also want to call click() or change() on the radio button afterwards. See comments for more info.

Code Snippets

$("#radio_1").prop("checked", true);
$("#radio_1").attr('checked', 'checked');

Context

Stack Overflow Q#5665915, score: 1705

Revisions (0)

No revisions yet.