Reputation: 1
I am trying to make specific radiobutton checked on page load and tried a lot of ways, but no one worked for me. This is a part of orderform on my webstore.
<input type="radio" name="sPaymentCarrier" id="pC_2-1" value="1;2;0.00"
onclick="countCarrierPrice( this )" />
<label for="pC_2-1">0</label>
Actually this radiobutton is the only on radiobutton on page, and it must be checked to let user make order.
I tried
$(function() {
var $radios = $('input:radio[name=sPaymentCarrier]');
if($radios.is(':checked') === false) {
$radios.filter('[value=1;2;0.00]').attr('checked', true);
}
});
and
$('input:radio[name="sPaymentCarrier"]').filter('[value="1;2;0.00"]').attr('checked', true);
but none of these ways worked. Also I tried a lot of other codes, but when I refreshed page I still saw this button unchecked. I guess maybe I have to set up jquery click on this button instead of select, but actually I cannot make this work.
Any help is appreciated!
Upvotes: 0
Views: 2253
Reputation: 3419
Use the val() function to set checkboxes or radiobuttons values to true.
Upvotes: 0
Reputation: 40863
Since I see you also have some javascript for onclick just do the following:
$('#pC_2-1').click();
This assumes you want to also run the onclick handler you defined on your element. If that is not the case setting the attr
mentioned in the other answers will work just as fine.
Side note, if you only have one radio button on the page, maybe a checkbox would be more appropriate?
Upvotes: 3
Reputation: 96914
The following should work:
$("#pC_2-1").attr("checked", "checked");
You can adapt the selector as you wish, but the checked
attribute needs to be set to "checked"
, not true
.
Upvotes: 0