Skip to content

Add disabled attribute JavaScript | Disable HTML elements Example

  • by

HTML webpages needed to disable/enable the button programmatically using JavaScript. Not only buttons are sometimes needed for input, checkbox, radio button, etc. Once you have the element reference, set its disabled property to true to disable it using JavaScript.

button.disabled = true

Add disabled attribute JavaScript Example

Disable Multiple HTML elements

For multiple elements use class and getElementsByClassName method with for-loop statement in JS.


    <input class="dummy" type="button"  value="Submit" />
    <input class="dummy" type="checkbox" name="checkbox">
    <input class="dummy" type="radio" name="">

    var elem = document.getElementsByClassName('dummy');
    for (var i = 0; i <= elem.length; i++) {
        elem[i].disabled = true;


HTML Add disabled attribute JavaScript

Disable Single element

Use id if you want to Add a disabled attribute on a single element.


    <input id="dummy" type="button"  value="Submit" />

    var elem = document.getElementById('dummy');

    elem.disabled = true;

Do comment if you have any doubts or suggestions on this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.