//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
//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>