How to disable radio button using JavaScript ?
Last Updated :
21 May, 2025
Radio buttons let users select one option from many, commonly used in forms. Sometimes, based on a user's previous choice, certain options need to be disabled. For example, if a user selects "No" to playing games, we can disable related game options. Using JavaScript, about 65% of forms use such logic to guide user input. In this article, you’ll learn how to disable radio buttons using the disabled
attribute with simple, real-life examples.
Example 1: In this example, we have two questions the first one is a "yes" or "no" question, if the user select "yes" the radio buttons will not be disabled if the user selects "no" then we will disable the radio buttons.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<!-- CSS code required for the page -->
<style>
body {
display: flex;
align-items: center;
justify-content: center;
margin-top: 10%;
background: rgb(51, 51, 51);
}
.main {
width: 60%;
height: 100%;
background-color: #f2f2f2;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 30px;
border-radius: 20px;
background-color: rgb(0, 0, 0);
color: white;
font-family: montserrat;
font-size: 0.8rem;
}
#submit {
width: 30%;
margin: 20px;
height: 40px;
border-radius: 20px;
background-color: rgb(0, 133, 7);
color: rgb(0, 0, 0);
font-family: montserrat;
font-size: 1rem;
font-weight: bold;
border: none;
cursor: pointer;
}
#submit:hover {
background-color: rgb(0, 98, 5);
color: rgb(0, 0, 0);
}
</style>
</head>
<body>
<div class="main">
<h1>Do you listen musing while coding?</h1>
<div class="question1">
<label>
<input name="music" type="radio"
id="yes" value="yes" /> Yes
</label>
<label>
<input name="music" type="radio" id="no"
value="no" onchange="check()" /> No
</label>
</div>
<h2>
If yes, What kind of music
do you prefer?
</h2>
<div class="question2">
<label>
<input type="radio" name="Musics"
id="Pop" value="pop" />Pop</label>
<label>
<input type="radio" name="Musics"
id="Rock" value="rock" />Rock</label>
<label>
<input type="radio" name="Musics"
id="Jazz" value="jazz" />Jazz</label>
<label>
<input type="radio" name="Musics"
id="Classical" value="classical" />
Classical
</label>
</div>
<button id="submit" onclick="message()">
submit
</button>
</div>
<script>
// This function will check if the user has
// selected any option from the question 1
function check() {
if (document.getElementById('no').checked) {
document.getElementById('Pop').disabled = true;
document.getElementById('Rock').disabled = true;
document.getElementById('Jazz').disabled = true;
document.getElementById('Classical').disabled = true;
}
else {
document.getElementById('Pop').disabled = false;
document.getElementById('Rock').disabled = false;
document.getElementById('Jazz').disabled = false;
document.getElementById('Classical').disabled = false;
}
}
// This function will give the message after
// the user clicks on the submit button.
function message() {
if (document.getElementById('yes').checked) {
if (document.getElementById('Pop').checked) {
alert("You like pop music");
}
else if (document.getElementById('Rock').checked) {
alert("You like rock music");
}
else if (document.getElementById('Jazz').checked) {
alert("You like jazz music");
}
else if (document.getElementById('Classical').checked) {
alert("You like classical music");
}
}
else {
alert("You don't listen musing while coding");
}
alert("Thank you for your response!");
}
</script>
</body>
</html>
Output:
Example 2: In this example, we will have three questions, on the basis of the first question we will disable one question from the remaining two questions and vice versa.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<!-- CSS code required for the page -->
<style>
body {
display: flex;
align-items: center;
justify-content: center;
margin-top: 10%;
background: rgb(51, 51, 51);
}
.main {
width: 60%;
height: 100%;
background-color: #f2f2f2;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 30px;
border-radius: 20px;
background-color: rgb(255, 255, 255);
color: rgb(27, 27, 27);
font-family: montserrat;
font-size: 0.8rem;
}
#submit {
width: 30%;
margin: 20px;
height: 40px;
border-radius: 20px;
background-color: rgb(34, 34, 34);
color: rgb(255, 255, 255);
font-family: montserrat;
font-size: 1rem;
font-weight: bold;
border: none;
cursor: pointer;
}
#submit:hover {
background-color: rgb(0, 98, 5);
color: rgb(0, 0, 0);
}
</style>
</head>
<body>
<div class="main">
<h1>What kind of meal do you prefer?</h1>
<div class="question">
<label> <input type="radio" name="meal"
id="veg" onchange="check()">Veg</label>
<label> <input type="radio" name="meal"
id="nonveg" onchange="check()">
Non-Veg
</label>
</div>
<h2>Veg menu</h2>
<div class="veg_menu">
<label> <input type="radio"
name="veg_meal" id="salad">Salad</label>
<label> <input type="radio"
name="veg_meal" id="soup">Soup</label>
<label> <input type="radio"
name="veg_meal" id="dessert">Dessert
</label>
</div>
<h2>Non-veg Menu</h2>
<div class="non-veg_menu">
<label> <input type="radio"
name="non_veg_meal" id="chicken">Chicken</label>
<label> <input type="radio" name="non_veg_meal"
id="mutton">Mutton</label>
<label> <input type="radio" name="non_veg_meal"
id="fish">Fish</label>
</div>
<button id="submit" onclick="message(),resetMessage()">
submit
</button>
</div>
<script>
function check() {
if (document.getElementById('veg').checked) {
document.getElementById('chicken').disabled = true;
document.getElementById('mutton').disabled = true;
document.getElementById('fish').disabled = true;
document.getElementById('salad').disabled = false;
document.getElementById('soup').disabled = false;
document.getElementById('dessert').disabled = false;
}
else if (document.getElementById('nonveg').checked) {
document.getElementById('salad').disabled = true;
document.getElementById('soup').disabled = true;
document.getElementById('dessert').disabled = true;
document.getElementById('chicken').disabled = false;
document.getElementById('mutton').disabled = false;
document.getElementById('fish').disabled = false;
}
}
function message() {
if (document.getElementById('veg').checked) {
if (document.getElementById('salad').checked) {
alert("You have selected Salad");
}
else if (document.getElementById('soup').checked) {
alert("You have selected Soup");
}
else if (document.getElementById('dessert').checked) {
alert("You have selected Dessert");
}
else {
alert("Please select a meal");
}
}
else if (document.getElementById('nonveg').checked) {
if (document.getElementById('chicken').checked) {
alert("You have selected Chicken");
}
else if (document.getElementById('mutton').checked) {
alert("You have selected Mutton");
}
else if (document.getElementById('fish').checked) {
alert("You have selected Fish");
}
else {
alert("Please select a meal");
}
}
else {
alert("Please select a meal");
}
}
</script>
</body>
</html>
Output:
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read