js get radio checked value code example
Example 1: get radio button value javascript
document.querySelector('input[name="rate"]:checked').value;
Example 2: javascript radio button value if checked
$test=document.querySelector('input[name = "comp"]:checked').value;
if($test="silver") {
amount=50;
}else if($test="gold") {
amount=90;
}else{
amount=30;
}
Example 3: js check if radio button is checked
if(document.getElementById('gender_Male').checked) {
}else if(document.getElementById('gender_Female').checked) {
}
Example 4: which radio button is selected javascript
$('#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>
Example 5: getting value from radio button javascript
var elements = document.getElementsByName('radioButton');
var checkedButton;
console.log(elements);
elements.forEach(e => {
if (e.checked) {
checkedButton = e.value;
}
});