Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get value from radio group using jquery

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.

like image 925
Rahul Singh Avatar asked Oct 11 '11 08:10

Rahul Singh


People also ask

How do you checked radio in jQuery?

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.

How do you check whether a Radiobutton is checked or not in Javascript?

Use document. getElementById('id'). checked method to check whether the element with selected id is check or not. If it is checked then display its corresponding result otherwise check the next statement.


2 Answers

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

like image 117
Null Pointer Avatar answered Sep 21 '22 14:09

Null Pointer


It works for me

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

Exaple here

like image 37
Gowri Avatar answered Sep 25 '22 14:09

Gowri