How to use the removeAttribute() method In Javascript

The ‘removeAttribute( )’ method in JavaScript is used to eliminate a specific attribute, such as “disabled,” from an HTML element, enabling user interaction.

Syntax:

document.getElementById('geeks').removeAttribute('disabled');

Example: This example implements the use of the removeAttribute() method to remove the disabled attribute from the HTML input element.

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        To remove “disabled” attribute from
        HTML input element
    </title>
    <style>
        body {
            text-align: center;
        }
 
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>w3wiki</h1>
    <p>Click on the button to remove disabled attribute</p>
    Type:
    <input id="input" disabled /><br><br>
    <button onclick="myGFG()">Click Here</button>
    <p id="gfg"></p>
    <script>
        let down = document.getElementById("gfg");
 
        function myGFG() {
            document.getElementById('input').removeAttribute('disabled');
            down.innerHTML = "Disabled Attribute removed";
        }
    </script>
</body>
 
</html>


Output:

How to remove “disabled” attribute from HTML input element using JavaScript ?

In HTML, we use input elements for user input. The ‘disabled’ property helps prevent interaction with these elements. For example, if we only want users over 18 to input their Adhar card numbers, we can use JavaScript to remove the ‘disabled’ attribute when the user enters an age greater than 18. This ensures that users can only input Adhar card numbers if they meet the specified age requirement. Here, we will learn various approaches to remove the disabled attribute from the HTML input element using JavaScript.

Table of Content

  • Using the removeAttribute() method
  • Modifying the ‘disabled’ property

Similar Reads

Using the removeAttribute() method

The ‘removeAttribute( )’ method in JavaScript is used to eliminate a specific attribute, such as “disabled,” from an HTML element, enabling user interaction....

Modifying the ‘disabled’ property

...