To get the value of 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 type as ‘radio’ and are selected are displayed.
Example 1: The following program displays the value of selected radio button when user clicks on Submit button.
<!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.
<!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:

Recommended Posts:
- How to check whether a radio button is selected with JavaScript ?
- How to know which radio button is selected using jQuery?
- How to unchecked a radio button using JavaScript/jQuery?
- How to create a radio button similar to toggle button using Bootstrap ?
- How to check a radio button with jQuery?
- Radio Button Vs Checkbox in HTML
- How to Create Custom Radio Button using HTML and CSS ?
- How to trigger HTML button after hitting enter button in textbox using JavaScript ?
- How to get the Highlighted/Selected text in JavaScript?
- How to get selected value in dropdown list using JavaScript ?
- How to get the ID of the clicked button using JavaScript / jQuery ?
- JavaScript | MouseEvent Button Property
- JavaScript | Trigger a button on ENTER key
- How to change the href value of <a> tag after click on button using JavaScript ?
- How to stop browser back button using JavaScript ?
- Sound generation on clicking the button using JavaScript
- How to move button in the same line with Checkbox and Textbox using JavaScript ?
- How to change the text and image by just clicking a button in JavaScript ?
- How to change the background color after clicking the button in JavaScript ?
- How to trigger a file download when clicking an HTML button or JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


