
223 views
How to disable radio button using JavaScript
To disable a radio button using JavaScript, you can set the disabled
property of the radio button element to true
. Here’s an example:
HTML
<input type="radio" id="radioButton" name="option" value="option1"> Option 1<br>
<input type="radio" name="option" value="option2"> Option 2<br>
<input type="radio" name="option" value="option3"> Option 3<br>
<script>
// Disable the radio button
var radioButton = document.getElementById("radioButton");
radioButton.disabled = true;
</script>
In this example, the radio button with the id
“radioButton” is selected using getElementById()
, and then the disabled
property is set to true
. This will disable the radio button, and the user won’t be able to select it.
You can use this approach to disable any radio button in your HTML document by selecting it with the appropriate method (getElementById()
, querySelector()
, etc.) and setting the disabled
property to true
.