Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to know which radio button is selected javascript

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

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

PREVIOUS NEXT
Code Example
Javascript :: jquery add html to end of div 
Javascript :: js add params to url 
Javascript :: remove duplicates from array js lodash 
Javascript :: lodash delete object property 
Javascript :: angular copy 
Javascript :: console.log color terminal 
Javascript :: split a string every n characters javascript 
Javascript :: javascript prompt for download location 
Javascript :: align text in js 
Javascript :: regex diferent of 
Javascript :: react absolute import 
Javascript :: jquery sum all input values 
Javascript :: js settimeout 
Javascript :: jquery get value by name 
Javascript :: v-for vue 
Javascript :: invalid chai property 
Javascript :: cors header missing vue api gateway 
Javascript :: regex para telefone celular 
Javascript :: redirect angular 
Javascript :: how to add bootstrap to vue js 
Javascript :: string to char array in javascript 
Javascript :: telli sense for jsx vscode 
Javascript :: pass array of strings to match in str.replace 
Javascript :: javascript loop through objec 
Javascript :: express messages 
Javascript :: onpress image react native 
Javascript :: toobject() javascript 
Javascript :: jquery first element 
Javascript :: get all keys of object in javascript 
Javascript :: nazmul hassan 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =