The problem statement is simple. I need to see if user has selected a radio button from a radio group. Every radio button in the group share same id.
The problem is
HTML Code:
<input type="radio" name="active_status" class="active_status" value="Hold">Hold
<input type="radio" name="active_status" class="active_status" value="Cancel">Cancel
<input type="radio" name="active_status" class="active_status" value="Suspend">Suspend
Jquery Code:
$(document).on("click", ".active_status", function () {
var a = $('input[name=active_status]:checked').val();
(OR)
var a = $('.active_status:checked').val();
alert(a);
});
You can get the value of selected radio button by Id using this in javascript/jQuery.
$("#InvCopyRadio:checked").val();
I hope this will help.
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();
By Name only
$(function () {
$('input[name="EventType"]:radio').change(function () {
alert($("input[name='EventType']:checked").val());
});
});
The best way to explain this simple topic is by giving simple example and reference link:-
In the following example we have two radio buttons. If the user selects any radio button, we will display the selected radio button value in a alert box.
Html:
<form id="Demo">
<input type="radio" name="Gender" value="Male" /> Male <br />
<input type="radio" name="Gender" value="Female" /> Female<br />
<input type="radio" name="Gender" value="others" /> others <br />
</form>
jquery:-
$(document).ready(function(){
$('#Demo input').on('change', function() {
alert($('input[name="Gender"]:checked', '#Demo').val());
});
});
Just use.
$('input[name="name_of_your_radiobutton"]:checked').val();
So easy it is.