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.

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 Change id of Element with JavaScript JavaScript Change li Text Color JavaScript Change li Text JavaScript Remove id from Element Check If id Exists in JavaScript How To Delete Table Row by id in JavaScript Update Textarea value in JavaScript Change Variable Value in JavaScript Uncheck Radio Button Dynamically JavaScript Check Radio Button Dynamically JavaScript JavaScript Check if Radio Button is Checked or Not Change h1 Tag Text with JavaScript