Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript is radio button checked

//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>
 
PREVIOUS NEXT
Tagged: #javascript #radio #button #checked
ADD COMMENT
Topic
Name
1+4 =