javascript - How can I know which radio button is selected via jQuery?

ID : 209

viewed : 124

Tags : javascriptjqueryhtmljquery-selectorsradio-buttonjavascript

Top 5 Answer for javascript - How can I know which radio button is selected via jQuery?

vote vote

92

To get the value of the selected radioName item of a form with id myForm:

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

Here's an example:

$('#myForm input').on('change', function() {     alert($('input[name=radioName]:checked', '#myForm').val());   });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <form id="myForm">    <input type="radio" name="radioName" value="1" /> 1 <br />    <input type="radio" name="radioName" value="2" /> 2 <br />    <input type="radio" name="radioName" value="3" /> 3 <br />  </form>

vote vote

82

Use this..

$("#myform input[type='radio']:checked").val(); 
vote vote

78

If you already have a reference to a radio button group, for example:

var myRadio = $("input[name=myRadio]"); 

Use the filter() function, not find(). (find() is for locating child/descendant elements, whereas filter() searches top-level elements in your selection.)

var checkedValue = myRadio.filter(":checked").val(); 

Notes: This answer was originally correcting another answer that recommended using find(), which seems to have since been changed. find() could still be useful for the situation where you already had a reference to a container element, but not to the radio buttons, e.g.:

var form = $("#mainForm"); ... var checkedValue = form.find("input[name=myRadio]:checked").val(); 
vote vote

61

This should work:

$("input[name='radioName']:checked").val() 

Note the "" usaged around the input:checked and not '' like the Peter J's solution

vote vote

57

You can use the :checked selector along with the radio selector.

 $("form:radio:checked").val(); 

Top 3 video Explaining javascript - How can I know which radio button is selected via jQuery?

Related QUESTION?