<input type="radio" value="True" propertyname="AmortizationTermInYears" onchange="showAmortizationTermInYears();UpdateField(this);" name="AmortizationTermInYears" id="AmortizationTermInYears" amortizationterminyearsradio="true">
<input type="radio" value="False" propertyname="AmortizationTermInYears" onchange="showAmortizationTermInYears();UpdateField(this);" name="AmortizationTermInYears" id="AmortizationTermInYears" checked="checked">
How can I select one of these radio buttons using Jquery when the page loads?
Thanks!
$('input[name=AmortizationTermInYears]:nth(0)').prop("checked","checked");
This will do the trick:
$(document).ready(function() {
$("input[name='AmortizationTermInYears'][value='True']").attr("checked", "checked");
});
You need to search by both name and value then set the checked
attribute.
Live test case.
Although the question is mostly about how to select an element, I do notice that everyone is using the attr()
function and, usually, this is wrong because calling this function only works once. If you call this, then the user checks a different one, if you call this the second time, it will not work. the correct way to do it is to use prop()
$('input[name=AmortizationTermInYears]:nth(0)').prop('checked', true);
"id" attribute should not be used again. You have given id="AmortizationTermInYears" to both radio buttons. It is wrong according to standards. Set different ids to both radio buttons.
Also you can select radio button by specifying id:
$('#AmortizationTermInYears').attr( "checked", "checked" );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With