get value from radio group using jquery

JqueryHtmlRadio Button

Jquery Problem Overview


I am trying to get value of radio group with name managerelradio. My html code for this radio group is.

 <label><input type="radio" name="managerelradio" value="Yes" id="Add">Add</label>
 <label><input type="radio" name="managerelradio" value="No" id="Remove">Remove</label>

and Jquery for this is..

	var manageradiorel = $('input[name = "managerelradio"]:checked' , '#managechildform').val();
 alert(manageradiorel);

its showing me undefined.

Though I have also tried it as.

 var manageradiorel = $('input[name = "managerelradio"]:checked').val();
 alert(manageradiorel);

But still I am getting undefined value.

Jquery Solutions


Solution 1 - Jquery

Try this

var manageradiorel = $("input:radio[name ='managerelradio']:checked").val();
alert(manageradiorel);

Plese check this DEMO ..it will work fine

Note: One of your radio button must be selected. Otherwise it will return undefined

You can use checked attribute to make a radio button selected as default

Solution 2 - Jquery

It works for me

$('input[name="managerelradio"]').on('change', function(e) {
    
    var manageradiorel = e.target.value;
    alert(manageradiorel);

});

Exaple here

Solution 3 - Jquery

A small jQuery extension to make this a little easier:

jQuery.fn.extend({
    groupVal: function() {
        return $(this).filter(':checked').val();
    }
});

// Usage:
$("input[name='managerelradio']").groupVal();

// Or even:
$("[name='managerelradio']").groupVal();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahul SinghView Question on Stackoverflow
Solution 1 - JqueryNull PointerView Answer on Stackoverflow
Solution 2 - JqueryGowriView Answer on Stackoverflow
Solution 3 - JqueryCupOfTea696View Answer on Stackoverflow