
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to Disable Radio Buttons using JavaScript?
A radio button is an input type that takes input in form of the options. The user will select one value from multiple choices. Only one option can be selected out of many possible choices.
In this article, we will explore how to disable a radio button and how to enable it back when required. We will see how radio buttons work internally and how can we disable them when required
Mostly radio buttons are shown to the user only in some cases as part of confirmation. When not required we can disable them so that the user does not choose an option.
Approach
In this approach, we will be using the disabled attribute provided by HTML. This attribute lets you disable any HTML element. This is a boolean-valued attribute that has only two values i.e. true or false. When the disabled is used it disables that particular HTML element. The default value of disabled is false.
Example
In the below example, we are comparing a string with a regex expression and returning its index if the string matches. If not, -1 will be returned.
# index.html
<!DOCTYPE html> <html lang="en"> <head> <!-- CSS code required for the page --> <style> #submit { width: 30%; margin: 20px; height: 40px; border-radius: 20px; color: black; font-size: 1rem; font-weight: bold; border: 1px solid black; cursor: pointer; } #submit:hover { background-color: grey; color: rgb(0, 0, 0); } </style> </head> <body> <div> <h1 style="color: green;">Welcome To Tutorials Point</h1> <h2>Do you like sports ?</h2> <div class="question1"> <label><input name="sport" type="radio" id="yes" value="yes"/> Yes</label> <label><input name="sport" type="radio" id="no" value="no" onchange="check()" /> No</label> </div> <h2> If No, Choose your favourite sport. </h2> <div class="question2"> <label><input type="radio" name="sports" id="cricket" value="Cricket" />Cricket</label> <label><input type="radio" name="sports" id="football" value="Football" />Football</label> <label><input type="radio" name="sports" id="basketball" value="Basket Ball" />Basket Ball</label> <label><input type="radio" name="sports" id="others" value="Others" /> Others</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('cricket').disabled = true; document.getElementById('football').disabled = true; document.getElementById('basketball').disabled = true; document.getElementById('others').disabled = true; } } // This function will give the message after // the user clicks on the submit button. function message() { if (document.getElementById('yes').checked) { if (document.getElementById('cricket').checked) { alert("You like Cricket."); } else if (document.getElementById('football').checked) { alert("You like Football."); } else if (document.getElementById('basketball').checked) { alert("You like Basket Ball."); } else if (document.getElementById('others').checked) { alert("You like Other sports."); } } else { alert("You donot like sports"); } } </script> </body> </html>
Output
For the question, “Do you like sports?”, if you select “Yes” then the radio button is not disabled but if you select “No” then the radio button is disabled.
- Related Articles
- How to dynamically create radio buttons using an array in JavaScript?
- How to disable Home and other system buttons in Android using Kotlin?
- How to get the total number of radio buttons on a page using Selenium?
- How to create custom checkboxes and radio buttons with CSS?
- How do we use radio buttons in HTML forms?
- How to disable Javascript when using Selenium?
- How to disable Home and other system buttons in Android?
- Custom Radio Buttons with CSS appearance Property
- How to uncheck a radio button using JavaScript/jQuery?
- How to disable resizable property of textarea using JavaScript?
- Preappend or Append radio buttons and checkboxes with Bootstrap
- How to disable mouse event on certain elements using JavaScript?
- How to disable right-clicking on a website using JavaScript?
- How to animate buttons using CSS?
- How to disable JavaScript in Firefox?
