How to use disabled attribute In Javascript

In this approach, we are using the disabled property of JavaScript to set the disabled attribute of the input field to render it in an uneditable form so that it cannot accept input from the users.

Syntax:

element.disabled = true;

Example: The below example uses a disabled attribute to disable input in JavaScript.

HTML




<!DOCTYPE html>
 
<head>
    <title>Example 1</title>
    <style>
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>w3wiki</h1>
    <h3>Using disabled attribute</h3>
    <input type="text" id="input1"
        value="w3wiki">
    <script>
        document.getElementById("input1").
        disabled = true;
    </script>
</body>
 
</html>


Output:

How to Disable Input in JavaScript ?

In JavaScript, disabling the input field mainly restricts the users from interacting with the input elements, and also prevents the modifications to the form elements such as buttons or input fields.

We can use various methods and attributes to disable the input.

Table of Content

  • Using disabled attribute
  • Using readOnly attribute

Similar Reads

Using disabled attribute

In this approach, we are using the disabled property of JavaScript to set the disabled attribute of the input field to render it in an uneditable form so that it cannot accept input from the users....

Using readOnly attribute

...

Contact Us