//html
<input type="radio" name="someName" value="value1">
<label>value1</label>
<input type="radio" name="someName" value="value2">
<label>value2</label>
//js
const radioButtons = document.querySelectorAll('input[name="someName"]');
var selectedRadioButton;
for (const radioButton of radioButtons) {
if (radioButton.checked) {
selectedRadioButton = radioButton.value;
break;
}
}
//now you have the value selected in selectedRadioButton
// Native JS solution:
document.getElementById("_1234").checked = true;
// JQuery solution:
$("#_1234").prop("checked", true);
if(document.getElementById('gender_Male').checked) {
//Male radio button is checked
}else if(document.getElementById('gender_Female').checked) {
//Female radio button is checked
}
//alert(document.querySelector('input[name = "comp"]:checked').value);
$test=document.querySelector('input[name = "comp"]:checked').value;
if($test="silver") {
amount=50;
}else if($test="gold") {
amount=90;
}else{
amount=30;
}
//get radio Buttons (other ways to do this too)
let radioButtons = document.querySelectorAll("input[name=myRadioName]");
//annoyingly the "change" event only fires when a radio button is clicked
//so it does not fire when another one is clicked and it gets unchecked
//so you have to put change listener on all radio buttons
//and then detect which one is actually selected
for (let i = 0; i < radioButtons.length; i++) {
radioButtons[i].addEventListener('change', function() {
if (radioButtons[i].checked == 1){
alert("checked") ;
} else {
alert("not checked")
}
});
}
//html look like this:
<form name="myFormName">
<input type="radio" name="myRadioName" value="1" />
<input type="radio" name="myRadioName" value="2" />
<input type="radio" name="myRadioName" value="3" />
</form>