[Solved] JQuery: How to get selected radio button value?

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?

Enquirer: StaceyH

||

Solution #1:

$('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
});
Respondent: John Hartsock

Solution #2:

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();
    });
}
Respondent: Dexter

Solution #3:

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();
});
Respondent: Ashish Babu

Solution #4:

jQuery("input:radio[name=myradiobutton]:checked").val();
Respondent: Muhammad Adnan

Solution #5:

Probably the best method (particularly if you want to be able to post a default value), would be to have a hidden radio button that starts out as selected and has your default value. So something like this:

<input type="radio" name="myradiobutton" value="0" checked="checked" style="display:none;" />
<input type="radio" name="myradiobutton" value="1" />1
<input type="radio" name="myradiobutton" value="2" />2
<input type="radio" name="myradiobutton" value="3" />3

If you can’t modify your html directly, you could add it via script:

$(function() {
    $('input[name=myradiobutton]:radio:first').before('<input type="radio" name="myradiobutton" value="0" checked="checked" style="display:none;" />');
});

Here’s a demo of that: http://jsfiddle.net/Ender/LwPCv/

Respondent: Ender

Solution #6:

I know this is an old question but I find that the answers are not sufficent enough

The cleanest way to do this is to use this code

$(".myRadio").click(function() {
alert($(this).val());   
});

🙂

Your form input data should look like this

<input type="radio" value="40000" name="pay"  class="myRadio" />
<label for="40000">40000</label>
Respondent: mindmyweb

Solution #7:

This Jquery method returns the default vale 0 when page loads…

$('input[type="radio"]:checked').val();
Respondent: arunsignit

Solution #8:

To get the value of the selected Radio Button, Use RadioButtonName and the Form Id containing the RadioButton.

$('input[name=radioName]:checked', '#myForm').val()

OR by only

$('form input[type=radio]:checked').val();
Respondent: Nadir

Solution #9:

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”.

Respondent: Gregg B

Solution #10:

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();
Respondent: saqibahmad

Solution #11:

For radio buttons use the following script:

var myRadio = $('input[name=meme_wall_share]');
var checkedValue = myRadio.filter(':checked').val();

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.