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
<div id="subscriptions">
Company Suscription: <input type="radio" name="subsrad" value="1">
Customer Subscription:<input type="radio" name="subsrad" value="2">
Manully Set:<input type="radio" name="subsrad" value="MANUAL">
NO Subscription:<input type="radio" name="subsrad" value="0">
</div>
and handle jquery for alert as for th e value set / Changed through div id:
$("#subscriptions input") // select the radio by its id
.on('change', function(){ // bind a function to the change event
alert($('input[name="subsrad"]:checked', '#subscriptions').val());
});
it is so easy....:-}
I am not a javascript person, but I found here for searching this problem. For who google it and find here, I am hoping that this helps some. So, as in question if we have a list of radio buttons:
<div class="list">
<input type="radio" name="b1" value="1">
<input type="radio" name="b2" value="2" checked="checked">
<input type="radio" name="b3" value="3">
</div>
I can find which one selected with this selector:
$('.list input[type="radio"]:checked:first').val();
Even if there is no element selected, I still don't get undefined error. So, you don't have to write extra if statement before taking element's value.
Here is very basic jsfiddle example.
$("input[name='gender']:checked").val()
for nested attributes
$("input[name='lead[gender]']:checked").val()
Don't forget single braces for name
I know that I am joining this late. But it is worth mentioning that it takes
<label class="custom-control-label" for="myRadioBtnName">Personal Radio Button</label>
And then I checked this in my js. It could be like
$(document).ready(function () {
$("#MyRadioBtnOuterDiv").click(function(){
var radioValue = $("input[name=myRadioButtonNameAttr]:checked").val();
if(radioValue === "myRadioBtnName"){
$('#showMyRadioArea').show();
}else if(radioValue === "yourRadioBtnName"){
$('#showYourRadioArea').show();
}
});
});
`
<input type="radio" name='s_2_1_6_0' value='Mail copy to my bill to address' id = "InvCopyRadio" onchange = 'SWESubmitForm(document.SWEForm2_0,s_4,"","1-DPWJJF")' style="height:20;width:25" tabindex=1997 >
$(function() {
$("#submit").click(function() {
alert($('input[name=s_2_1_6_0]:checked').val());
});
});`
if (!$("#InvCopyRadio").prop("checked") && $("#InvCopyRadio").prop("checked"))
// do something