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
multi group radio button covert value to array
var arr = [];
function r(n) {
var section = $('input:radio[name="' + n + '"]:checked').val();
arr[n] = section;
console.log(arr)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" onchange="r('b1')" class="radioID" name="b1" value="1">1
<input type="radio" onchange="r('b1')" class="radioID" name="b1" value="2" >2
<input type="radio" onchange="r('b1')" class="radioID" name="b1" value="3">3
<br>
<input type="radio" onchange="r('b2')" class="radioID2" name="b2" value="4">4
<input type="radio" onchange="r('b2')" class="radioID2" name="b2" value="5" >5
<input type="radio" onchange="r('b2')" class="radioID2" name="b2" value="6">6
Here are 2 radio buttons namely rd1 and Radio1
<input type="radio" name="rd" id="rd1" />
<input type="radio" name="rd" id="Radio1" />
The simplest wayt to check which radio button is checked ,by checking individual is(":checked") property
if ($("#rd1").is(":checked")) {
alert("rd1 checked");
}
else {
alert("rd1 not checked");
}
In case you don't know the sepcific name or want to check all radio inputs in a form, you can use a global var to check for each radio group the value only once: `
var radio_name = "";
$("form).find(":radio").each(function(){
if (!radio_name || radio_name != $(this).attr('name')) {
radio_name = $(this).attr('name');
var val = $('input[name="'+radio_name+'"]:checked').val();
if (!val) alert($('input[name="'+radio_name+'"]:checked').val());
}
});`
Check the example it works fine
<div class="dtl_radio">
Metal purity :
<label>
<input type="radio" name="purityradio" class="gold_color" value="92" checked="">
92 %
</label>
<label>
<input type="radio" name="purityradio" class="gold_color" value="75">
75 %
</label>
<label>
<input type="radio" name="purityradio" class="gold_color" value="58.5">
58.5 %
</label>
<label>
<input type="radio" name="purityradio" class="gold_color" value="95">
95 %
</label>
<label>
<input type="radio" name="purityradio" class="gold_color" value="59">
59 %
</label>
<label>
<input type="radio" name="purityradio" class="gold_color" value="76">
76 %
</label>
<label>
<input type="radio" name="purityradio" class="gold_color" value="93">
93 %
</label>
</div>
var check_value = $('.gold_color:checked').val();
:checked
.$(function(){
$("#submit").click(function() {
alert($("input[name=q12_3]:checked").val());
});
});
.is(":checked")
.
jQuery's is()
function returns a boolean (true or false) and not an
element.HTML Code
<input id="is_verified" class="check" name="is_verified" type="radio" value="1"/>
<input id="is_verified" class="check" name="is_verified" type="radio" checked="checked" value="0"/>
<input id="submit" name="submit" type="submit" value="Save" />
Javascript Code
$("input[type='radio'].check").click(function() {
if($(this).is(':checked')) {
if ($(this).val() == 1) {
$("#submit").val("Verified & Save");
}else{
$("#submit").val("Save");
}
}
});