Open In App

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

Last Updated : 03 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a form containing a 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 the selected id is checked 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: 

html




<!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 selected 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 display its corresponding result. If no one radio button is selected then it returns ‘No one selected’ 

Example: 

html




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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads