how to set radio option checked onload with jQuery how to set radio option checked onload with jQuery jquery jquery

how to set radio option checked onload with jQuery


Say you had radio buttons like these, for example:

<input type='radio' name='gender' value='Male'><input type='radio' name='gender' value='Female'>

And you wanted to check the one with a value of "Male" onload if no radio is checked:

$(function() {    var $radios = $('input:radio[name=gender]');    if($radios.is(':checked') === false) {        $radios.filter('[value=Male]').prop('checked', true);    }});


How about a one liner?

$('input:radio[name="gender"]').filter('[value="Male"]').attr('checked', true);


This one will cause form.reset() failure:

$('input:radio[name=gender][value=Male]').attr('checked', true);

But this one works:

$('input:radio[name=gender][value=Male]').click();