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 check radio button

// Native JS solution:
document.getElementById("_1234").checked = true;
// JQuery solution:
$("#_1234").prop("checked", true);
Comment

js check if radio button is checked

if(document.getElementById('gender_Male').checked) {
  //Male radio button is checked
}else if(document.getElementById('gender_Female').checked) {
  //Female radio button is checked
}
Comment

javascript radio button value if 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;
    }
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 :: angular contains both .browserslistrc and browserslist 
Javascript :: javascript submit a form with id 
Javascript :: sequelize get only one column 
Javascript :: Truncate a string-Javascript 
Javascript :: Javascript switch case code format 
Javascript :: formik clear field 
Javascript :: how to get custom attribute value in react 
Javascript :: string to in js 
Javascript :: js get element type 
Javascript :: firebase.database is not a function 
Javascript :: vue property decorator emit 
Javascript :: javascript function convert bytes into mb 
Javascript :: cookie js 
Javascript :: how to return json response in flask 
Javascript :: clear localstorage on click jquery 
Javascript :: make select option selected javascript 
Javascript :: how to use absolute path in react 
Javascript :: nuxt head script content 
Javascript :: set drain docker node 
Javascript :: discord js convert timestamp to date 
Javascript :: integers to space separated string in javascript 
Javascript :: lodash pascal case 
Javascript :: electron no menu bar 
Javascript :: how to make div visible and invisible in javascript 
Javascript :: get monitor width javascript 
Javascript :: bodyparser purpose 
Javascript :: xhr 
Javascript :: redux devtools extension npm 
Javascript :: swiftyjson 
Javascript :: event listener javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =