JavaScript Check if Radio Button is Checked or Not

In this tutorial we will learn How To Check if Radio Button is Checked or Not with JavaScript. JavaScript querySelectorAll() method and checked property are used to check the radio button state.

querySelectorAll() method

JavaScript querySelectorAll() method selects or targets the HTML elements who match with the CSS selector used.

In this example querySelectorAll() method is used to select all radio buttons with the help of name.

checked property

JavaScript checked property either checks or unchecks the radio button or HTML checkbox or it is used to check the current state of these two input elements.

In this example checked property is used to check if radio button is checked or not.

HTML Code

HTML Code is given below, in this code we have two radio buttons with two labels and a button tag with onclick event to execute a JavaScript function.

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Check if Radio Button is Checked or Not</title>
</head>
<body>

<label for="m1">Male</label>    
<input type="radio" id="m1" value="male" name="gender">
<label for="f1">Female</label>
<input type="radio" id="f1" value="female" name="gender">
<button onclick='checkRadio()'>Check Radio Button</button>    
    
<script src="script.js"></script>    
</body>
</html>

JavaScript Code

Take a look at the JavaScript code, in this code querySelectorAll() method is used to select the radio button with name = gender.

Since there are more than one radio button so we have used for loop to check all radio buttons if they are checked or not.

The loop will break if any radio button is found checked. This is done using break statement to make sure that the alert message is only displayed once. At the same time value of variable a is changed to make sure that second if block doesn't execute.

If the value of a doesn't change and remains 0, then it means none of the radio buttons is checked and all are unchecked. Thus the related message (in second if statement) will be displayed.

<script>
var a=0;
const radio = document.querySelectorAll('input[name="gender"]');
function checkRadio()
{
    for (var i = 0; i < radio.length; i++)
    {
      if (radio[i].checked) 
      {
        alert('Checked!');
        a =1;  
        break;   
      }
    }
    if(a==0)
    {
     alert('Not Checked!');
    }
}
</script>

Demo

Video Tutorial

Watch video tutorial on How To Check if Radio Button is Checked or Not in JavaScript.

Change font-size using JavaScript Get Font Size of any Tag using JavaScript Change Position of HTML Element using JavaScript How to Change Image on Hover with JavaScript How to Disable Button by Class Name in JavaScript How To Change Image Size with JavaScript How to change Image opacity with JavaScript How to Change image src on click with JavaScript How to get the lang attribute value in JavaScript How to Get attribute Value using JavaScript How To Check if Attribute Exists or not with JavaScript How To Count number of links on Page with JavaScript How To Scroll Page to Bottom using JavaScript How To Detect Shift Key Press with JavaScript Change Text Color On Hover with JavaScript Hide and Show div using JavaScript Get Button text with JavaScript Get textarea value with JavaScript Get table row Height with JavaScript Auto Increase width of input field with JavaScript Set Textarea maxLength with JavaScript Set Textarea Value with JavaScript JavaScript Count list items JavaScript set input field value Count Button Clicks with JavaScript Scroll Page to the Top on Click with JavaScript