Skip to content
Related Articles
Open in App
Not now

Related Articles

How to get value of selected radio button using JavaScript?

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 10 Nov, 2022
Improve Article
Save Article

To get the value of the selected radio button, a user-defined function can be created that gets all the radio buttons with the name attribute and finds the radio button selected using the checked property. The checked property returns True if the radio button is selected and False otherwise. If there are multiple Radio buttons in a webpage, first, all the input tags are fetched and then the values of all the tags that have typed as ‘radio’ and are selected are displayed. 

Example 1: The following program displays the value of the selected radio button when the user clicks on Submit button. 

html




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


Output : 

Example 2: The following program displays values of all the selected radio buttons when submit is clicked. 

html




<!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>
      
    Food Preference:
    <input type="radio" name="Food" value="Vegetarian">Vegetarian
    <input type="radio" name="Food" value="Non-Vegetarian">Non-Vegetarian
      
    <br>
      
    <button type="button" onclick="displayRadioValue()">
        Submit
    </button>
      
    <br>
      
    <div id="result"></div>
      
    <script>
        function displayRadioValue() {
            document.getElementById("result").innerHTML = "";
            var ele = document.getElementsByTagName('input');
              
            for(i = 0; i < ele.length; i++) {
                  
                if(ele[i].type="radio") {
                  
                    if(ele[i].checked)
                        document.getElementById("result").innerHTML
                                += ele[i].name + " Value: "
                                + ele[i].value + "<br>";
                }
            }
        }
    </script>
</body>
  
</html>                    


Output: 

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!