get radio button value javascript
snippet in javascript

get radio button value javascript

user5239

document.querySelector('input[name="rate"]:checked').value;

getting value from radio button javascript

user5299

    var elements = document.getElementsByName('radioButton');
    var checkedButton;
    console.log(elements);
    elements.forEach(e => {
        if (e.checked) {
            //if radio button is checked, set sort style
            checkedButton = e.value;
        }
    });

getting value from radio button javascript

user1282

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        Get value of selected 
        radio button 
    </title> 
</head> 
  
<body> 
    <p> 
        Select a radio button and click on Submit. 
    </p> 
      
    Gender: 
    <input type="radio" name="gender" value="Male">Male 
    <input type="radio" name="gender" value="Female">Female 
    <input type="radio" name="gender" value="Others">Others 
      
    <br> 
      
    <button type="button" onclick="displayRadioValue()"> 
        Submit 
    </button> 
      
    <br> 
      
    <div id="result"></div> 
      
    <script> 
        function displayRadioValue() { 
            var ele = document.getElementsByName('gender'); 
              
            for(i = 0; i < ele.length; i++) { 
                if(ele[i].checked) 
                document.getElementById("result").innerHTML
                        = "Gender: "+ele[i].value; 
            } 
        } 
    </script> 
</body> 
</html>                     

javascript on click radio button get value

user9571

if (document.getElementById('r1').checked) {
  rate_value = document.getElementById('r1').value;
}