I have two radio buttons and want to post the value of the selected one. How can I get the value with jQuery?
I can get all of them like this:
$("form :radio")
How do I know which one is selected?
I have two radio buttons and want to post the value of the selected one. How can I get the value with jQuery?
I can get all of them like this:
$("form :radio")
How do I know which one is selected?
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">
<fieldset>
<legend>Choose radioName</legend>
<label><input type="radio" name="radioName" value="1" /> 1</label> <br />
<label><input type="radio" name="radioName" value="2" /> 2</label> <br />
<label><input type="radio" name="radioName" value="3" /> 3</label> <br />
</fieldset>
</form>
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();
You can use the :checked selector along with the radio selector.
$("form:radio:checked").val();
JQuery to get all the radio buttons in the form and the checked value.
$.each($("input[type='radio']").filter(":checked"), function () {
console.log("Name:" + this.name);
console.log("Value:" + $(this).val());
});
Another way to get it:
$("#myForm input[type=radio]").on("change",function(){
if(this.checked) {
alert(this.value);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myForm">
<span><input type="radio" name="q12_3" value="1">1</span><br>
<span><input type="radio" name="q12_3" value="2">2</span>
</form>
Try
myForm.myOption.value
function check() {
console.log( myForm.myOption.value );
}
<form id="myForm">
<input type="radio" name="myOption" value="1"> 1 <br>
<input type="radio" name="myOption" value="2"> 2 <br>
<input type="radio" name="myOption" value="3"> 3 <br>
</form>
<button onclick="check()">check</button>
You need access with the :checked selector:
a example:
$('input[name=radioName]:checked', '#myForm').val()
$('#myForm input').on('change', function() {
$('#val').text($('input[name=radioName]:checked', '#myForm').val());
});
#val {
color: #EB0054;
font-size: 1.5em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>Radio value: <span id='val'><span></h3>
<form id="myForm">
<input type="radio" name="radioName" value="a"> a <br>
<input type="radio" name="radioName" value="b"> b <br>
<input type="radio" name="radioName" value="c"> c <br>
</form>
How about this?
Using change and get the value of radio type is checked...
$('#my-radio-form').on('change', function() {
console.log($('[type="radio"]:checked').val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<form id="my-radio-form">
<input type="radio" name="input-radio" value="a" />a
<input type="radio" name="input-radio" value="b" />b
<input type="radio" name="input-radio" value="c" />c
<input type="radio" name="input-radio" value="d" />d
</form>
**Please try below example to check which radio button in selected **
<script>
$('#form1 input').on('change', function() {
alert($('input[name=age]:checked', '#form1 ').val());
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
<form id="form1">
<input type="radio" name="age" value="18" /> 18 <br />
<input type="radio" name="age" value="20" /> 20 <br />
<input type="radio" name="age" value="22" /> 22 <br />
</form>
This solution does not require jQuery.
const RADIO_NAME = "radioName";
const radios = Array.from(document.getElementsByName(RADIO_NAME));
const checkedRadio = radios.filter(e=>e.checked);
This uses jQuery:
const radios = Array.from($(`[name=${RADIO_NAME}`));
const checkedRadio = radios.filter(e=>e.checked);
jQuery adds an extra layer of abstraction that isn't needed here.
You could also use:
const radios = Array.from(document.querySelectorAll(`[name=${RADIO_NAME}`));
const checkedRadio = radios.filter(e=>e.checked)[0];
But getElementsByName is simple and clear enough.
jQuery plugin for setting and getting radio-button values. It also respects the "change" event on them.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="toggle-form">
<div id="radio">
<input type="radio" id="radio1" name="radio" checked="checked" /><label for="radio1">Plot single</label>
<input type="radio" id="radio2" name="radio"/><label for="radio2">Plot all</label>
</div>
</form>
<script type="text/javascript">
$( document ).ready(function() {
//Get all radios:
var radios = jQuery("input[type='radio']");
checked_radios=radios.filter(":checked");
for(i=0;i<checked_radios.length;i++)
{
console.log(checked_radios[i]);
}
});
</script>
or another way
<script type="text/javascript">
$( document ).ready(function() {
//Get all radios:
checked_radios=jQuery('input[name=radio]:checked').val();
for(i=0;i<checked_radios.length;i++)
{
console.log(checked_radios[i]);
}
});
</script>
Along with the CSS selector :checked, you can also use the prop function (as of jQuery 1.6). I can't remember what project I was working on where simply using $('#something').is(':checked') only worked sometimes, and I resorted to also using $('#something').prop('checked') worked when it failed, but it led me to using both.
In my code snippet below, I've written two helper functions, is_checked and get_group_value. The function is_checked returns a boolean true/false value; true if the input passed in the parameter is checked (also checks with the prop() function) or false if it's not checked. The function get_group_value takes the name of the radio inputs and returns the value of the one that is checked, or an empty string if none are checked. These helper functions will also work with checkboxes, not just radio buttons.
Since the question did not define when they're retrieving the value(s), I've written a few listeners for four (3) different scenarios: when interacting with any radio button, when submitting the form, and when clicking one of these hard-coded buttons to do a one-time retrieval of the value of the group.
Please note that I'm using "click" to identify when the user interacts with the radio input element because "change" will never get triggered since the "value" attribute doesn't get changed when it's checked or not. I use this for checkboxes as well as radio buttons.
function is_checked(input) {
var $input = $(input);
return $input.is(':checked') || $input.prop('checked'); //Returns a boolean value. True if checked, false if not.
}
function get_group_value(group_name) {
var $inputs = $('[name="' + group_name + '"]:checked');
if ($inputs.length) { return $inputs.first().val(); } //If it exists, return the value of the first one found
return ''; //If it doesn't exist, return nothing
}
$('form').on('submit', function(e) {
e.preventDefault();
var $form = $(this), results = {};
$form.find('input[type=radio]').each(function() {
var $input = $(this);
if (is_checked(this)) {
results[$input.attr('name')] = $input.val();
}
});
console.info('Form Results', results);
});
$('form input[type=radio]').on('click', function(e) {
var group_name = $(this).attr('name');
console.info('Radio Button Click', group_name, get_group_value(group_name));
});
$('button.radio-button').on('click', function(e) {
var group_name = $(this).attr('id');
console.info('Button Click', group_name, get_group_value(group_name));
});
.my-test {
background: #ffffff;
color: #000000;
padding: 16px;
}
form {
padding: 8px;
border: 1px solid #999999;
}
fieldset {
border: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="my-test">
<form>
Group 1
<fieldset>
<label><input type="radio" name="example-1" value="Foo" required />Foo</label>
<label><input type="radio" name="example-1" value="Bar" required />Bar</label>
</fieldset>
Group 2
<fieldset>
<label><input type="radio" name="example-2" value="Banana" required />Banana</label>
<label><input type="radio" name="example-2" value="Apple" required />Apple</label>
</fieldset>
<button type="submit">Submit</button>
</form>
<p>Press this button to just get the value of the first group: <button class="radio-button" id="example-1">Foo or Bar?</button></p>
<p>Press this button to just get the value of the second group: <button class="radio-button" id="example-2">Banana or Apple?</button></p>
</div>
Using $('input[name="radioName"]').filter(":checked").val(); is the best way for me.
Tested, this works
Form
<form id="myForm">
<input type="radio" name="radioName" value="Option1"> Option1
<input type="radio" name="radioName" value="Option2" checked> Option2
<input type="radio" name="radioName" value="Option3"> Option3
</form>
Jquery
$(document).ready(function() {
$('input[name="radioName"]').on('change', function() {
const val = $(this).filter(":checked").val();
alert(val);
})
// First load check
const val = $('input[name="radioName"]').filter(":checked").val();
alert(val);
});
Example here: https://codepen.io/abinhho/pen/mdLrqbX
You can call Function onChange()
<input type="radio" name="radioName" value="1" onchange="radio_changed($(this).val())" /> 1 <br />
<input type="radio" name="radioName" value="2" onchange="radio_changed($(this).val())" /> 2 <br />
<input type="radio" name="radioName" value="3" onchange="radio_changed($(this).val())" /> 3 <br />
<script>
function radio_changed(val){
alert(val);
}
</script>