How do I default the value of a non-selected radio button to 0
?
I have the following HTML:
jQuery("input:radio[name=myradiobutton]:checked").val();
You should really be using checkboxes if there will be an instance where something isn't selected.
according to the W3C
If no radio button in a set sharing the same control name is initially "on", user agent behavior for choosing which control is initially "on" is undefined. Note. Since existing implementations handle this case differently, the current specification differs from RFC 1866 ([RFC1866] section 8.1.2.4), which states:
At all times, exactly one of the radio buttons in a set is checked. If none of the elements of a set of radio buttons specifies `CHECKED', then the user agent must check the first radio button of the set initially.
Since user agent behavior differs, authors should ensure that in each set of radio buttons that one is initially "on".
It will start as soon as the page loads. You can keep it under some events like button click
$("#btn").click(function() {
var val= $('input[type="radio"]:checked').val();
});
This work for me hope this will help: to get radio selected value you have to use ratio name as selector like this
selectedVal = $('input[name="radio_name"]:checked').val();
selectedVal will have the required value, change the radio_name according to yours, in your case it would b "myradiobutton"
selectedVal = $('input[name="myradiobutton"]:checked').val();
This Jquery method returns the default vale 0 when page loads...
$('input[type="radio"]:checked').val();