How do I default the value of a non-selected radio button to 0
?
I have the following HTML:
<input type="radio" name="myradiobutton" value="1" />1 <input type="radio" name="myradiobutton" value="2" />2 <input type="radio" name="myradiobutton" value="3" />3
And I have the following code to get the value of the selected radio button
var radio_button_value $('input[name=myradiobutton]:radio').click(function() {var value = $(this).val();});
Problem is, I want to default the value of radio_button_value
to 0
in the event nothing is selected. How do I do that?
I basically want to do the pseduo-code below (but this doesn't work)
var radio_button_value $('input[name=myradiobutton]:radio').click(function() { if set return $(this).val(); else return 0; });
UPDATE
There appears to be confusion on what I'm asking for.
What I want to do when the page loads (before a user has had a chance to even select a radio button), I want a function that will return 0 for the radio button value.
Then, once a user selected a radio button, that SAME function will return the selected value of the radio button.
Make sense?
You can simply use the jQuery :checked selector in combination with the val() method to find the value of the selected radio button inside a group.
We can check the status of a radio button by using the :checked jQuery selector together with the jQuery function is . For example: $('#el').is(':checked') . It is exactly the same method we use to check when a checkbox is checked using jQuery.
Build A Paint Program With TKinter and Python The radiobutton has only two values, either True or False. If we want to get the output to check which option the user has selected, then we can use the get() method. It returns the object that is defined as the variable.
$('input[name=myradiobutton]:radio:checked')
will get you the selected radio button $('input[name=myradiobutton]:radio:not(:checked)')
will get you the unselected radio buttons
Using this you can do this
$('input[name=myradiobutton]:radio:not(:checked)').val("0");
Update: After reading your Update I think I understand You will want to do something like this
var myRadioValue; function radioValue(jqRadioButton){ if (jqRadioButton.length) { myRadioValue = jqRadioButton.val(); } else { myRadioValue = 0; } } $(document).ready(function () { $('input[name=myradiobutton]:radio').click(function () { //Hook the click event for selected elements radioValue($('input[name=myradiobutton]:radio:checked')); }); radioValue($('input[name=myradiobutton]:radio:checked')); //check for value on page load });
Use the :checked selector to determine if a value is selected:
function getRadioValue () { if( $('input[name=myradiobutton]:radio:checked').length > 0 ) { return $('input[name=myradiobutton]:radio:checked').val(); } else { return 0; } }
Update you can call the function above at any time to get the selected value of the radio buttons. You can hook into it on load and then whenever the value changes with the following events:
$(document).ready( function() { // Value when you load the page for the first time // Will return 0 the first time it's called var radio_button_value = getRadioValue(); $('input[name=myradiobutton]:radio').click( function() { // Will get the newly selected value radio_button_value = getRadioValue(); }); }
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