How to check whether a radio button is selected with JavaScript ? - GeeksforGeeks

26 Jul, 2021
Given a form containing radio button element and the task is to check whether a radio button is selected or not with the help of JavaScript. There are two methods to solve this problem which are discussed below:
Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button.Use document.getElementById(‘id’).checked method to check whether the element with selected id is check or not. If it is checked then display its corresponding result otherwise check the next statement. If no one radio button is selected then it returns ‘No one selected’.
Example:
<!DOCTYPE html><html>  <head>    <title>        How to check whether a radio button        is selected with JavaScript ?    </title></head>  <body>           <h1 style="color:green;">        GeeksforGeeks    </h1>          <h3>        Click on the button to check whether<br>        a radio button is sected or not    </h3>          <form>        <input type="radio" name="GFG" id="GFG"            value="GeeksforGeeks">GeeksforGeeks<br>                  <input type="radio" name="GFG" id="HTML"            value="HTML">HTML<br>                  <input type="radio" name="GFG" id="JS"            value="JavaScript">JavaScript<br><br>              <button type="button" onclick="display()">            Submit        </button>    </form>    <br>    <div id="disp" style=        "color:green; font-size:18px; font-weight:bold;">    </div> </body>     <script>        function display() {             if(document.getElementById('GFG').checked) {                document.getElementById("disp").innerHTML                    = document.getElementById("GFG").value                    + " radio button checked";            }            else if(document.getElementById('HTML').checked) {                document.getElementById("disp").innerHTML                    = document.getElementById("HTML").value                    + " radio button checked";              }            else if(document.getElementById('JS').checked) {                document.getElementById("disp").innerHTML                    = document.getElementById("JS").value                    + " radio button checked";              }            else {                document.getElementById("disp").innerHTML                    = "No one selected";            }        }    </script></html>       
Output:
Before click on submit button:
After click on submit button:
Using DOM querySelector() Method: The querySelector() method is used to return the first element that matches the specified CSS selectors in the document.Use document.querySelector(‘input[name=”GFG”]:checked’) method to check the checked element of radio button and dislapy its corresponding result. If no one radio button is selected then it returns ‘No one selected’
Example:
<!DOCTYPE html><html>  <head>    <title>        How to check whether a radio button        is selected with JavaScript ?    </title></head>  <body>           <h1 style="color:green;">        GeeksforGeeks    </h1>          <h3>        Click on the button to check whether<br>        a radio button is sected or not    </h3>          <form>        <input type="radio" name="GFG" id="GFG"            value="GeeksforGeeks">GeeksforGeeks<br>                  <input type="radio" name="GFG" id="HTML"            value="HTML">HTML<br>                  <input type="radio" name="GFG" id="JS"            value="JavaScript">JavaScript<br><br>              <button type="button" onclick="display()">            Submit        </button>    </form>    <br>    <div id="disp" style=        "color:green; font-size:18px; font-weight:bold;">    </div> </body>     <script>        function display() {             var checkRadio = document.querySelector(                'input[name="GFG"]:checked');                          if(checkRadio != null) {                document.getElementById("disp").innerHTML                    = checkRadio.value                    + " radio button checked";            }            else {                document.getElementById("disp").innerHTML                    = "No one selected";            }        }    </script></html>
Output:
Before click on submit button:
After click on submit button:
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.
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Convert a string to an integer in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?