views:

88

answers:

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

How do I make this checked in JQUERY?

+10  A: 
$('#idOfMyRadio').attr('checked',true);
Jage
A: 

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;
Tim Down
I suppose I should have predicted I'd get a downvote for this, but please, if you do downvote, it's polite to leave a comment explaining why.
Tim Down
A: 

In addition to using id...

$('input[name="sort"]').attr('checked',true);
$('input[value="2"]').attr('checked',true);
CarolinaJay65
-1. Both will almost certainly be slower than using the id selector. The id selector can optimize and use `document.getElementById`, which is fast, while these will have to iterate over all the inputs in the document and check attribute values for each. Also, it's less precisely targetted: other radio buttons in the page that you're not intentionally targetting could perfectly well have a `name` of "sort" or a `value` of 2.
Tim Down