TIMEX
TIMEX

Reputation: 271884

How do I make an input radio button checked...using JQuery?

<input type="radio" name="sort" value="2" id="myradio">

How do I make this checked in JQUERY?

Upvotes: 12

Views: 28614

Answers (4)

Atif Tariq
Atif Tariq

Reputation: 2772

$('#idOfMyRadio').prop('checked',true);

Upvotes: 1

Tim Down
Tim Down

Reputation: 324597

Really, there's no need for jQuery here: it will be slower, introduce a needless framework dependency and increase the possibility of error. The DOM method for this is also as clear and readable as it could be, and works in all the major browsers back to the 1990s:

document.getElementById("myradio").checked = true;

If you must use jQuery then I'd stop at just using it to get hold of the element:

$("#myradio")[0].checked = true;

Upvotes: 6

Jay Corbett
Jay Corbett

Reputation: 28411

In addition to using id...

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

Upvotes: 5

Jage
Jage

Reputation: 8086

$('#idOfMyRadio').attr('checked',true);

Upvotes: 37

Related Questions