Skip to content
Related Articles

Related Articles

How to check whether a radio button is selected with JavaScript ?
  • Last Updated : 11 Oct, 2019

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:



My Personal Notes arrow_drop_up
Recommended Articles
Page :