In this tutorial we will learn How To Uncheck Radio Button Dynamically with JavaScript. JavaScript getElementById() method, onclick event and checked property can be used to uncheck radio button dynamically.
Table of Contents
getElementById() method
JavaScript getElementById() method selects or targets the HTML element using it's id.
In this example getElementById() method is used to select the radio button with help of it's id.
onclick event
onclick event triggers the click event for HTML tag or it attaches the JavaScript function with click event.
In this example onclick event is used to execute a JavaScript function on click of button to dynamically uncheck the radio button.
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 uncheck the radio button.
HTML Code
HTML Code is given below, in this code we have two radio buttons with two labels and two button tags with onclick event to execute two JavaScript functions.
<!DOCTYPE html>
<html>
<head>
<title>Unheck Radio Button Dynamically with JavaScript</title>
</head>
<body>
<label for="male">Male</label>
<input type="radio" id="male" value="male" name="gender">
<label for="female">Female</label>
<input type="radio" id="female" value="female" name="gender">
<br>
<button onclick="uncheckMale()">Uncheck Male</button>
<button onclick='uncheckFemale()'>Uncheck Female</button>
<script src="script.js"></script>
</body>
</html>
JavaScript Code
Take a look at the JavaScript code, in this code getElementById() method is used to select the radio button and checked property is used to uncheck radio button.
Two separate functions are used to uncheck both radio buttons.
<script>
function uncheckMale()
{
var radio = document.getElementById('male');
radio.checked = false;
}
function uncheckFemale()
{
var radio = document.getElementById('female');
radio.checked = false;
}
</script>
Demo
Video Tutorial
Watch video tutorial on How To Uncheck Radio Button with JavaScript.