jQuery | disable/enable an input element

The disable/enable an input element in jQuery can be done by using prop() method. The prop() method is used to set or return properties and values for the selected elements.

Example 1: This example uses prop() method to disable the input text field.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>  
<html>  
  
<head
    <title
        JavaScript enable/disable
        an input element
    </title>
      
    <script src=
    </script>
</head
          
<body style = "text-align:center;">  
  
    <h1 style = "color:green;" >  
        GeeksForGeeks  
    </h1>  
              
    <input id = "input" type="text" name="input"/> 
          
    <button onclick="enable_disable()"
        Enable/Disable
    </button
          
    <!-- Script to disable input text area -->
    <script
        function enable_disable() {
            $("input").prop('disabled', true);
        }
    </script
</body>  
  
</html>

chevron_right


Output:

  • Before clicking the button:
  • After clicking the button:

Example 2: This example uses prop() method to enable the input text field.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>  
<html>  
  
<head
    <title
        JavaScript enable/disable
        an input element
    </title>
      
    <script src=
    </script>
</head
          
<body style = "text-align:center;">  
  
    <h1 style = "color:green;" >  
        GeeksForGeeks  
    </h1
      
    <input id = "input" type="text" name="input" disabled/> 
      
    <button onclick="enable_disable()"
        Enable/Disable
    </button
      
    <!-- Script to enable input text fields -->       
    <script
        function enable_disable() {
            $("input").prop('disabled', false);
        }
    </script
</body>  
  
</html>

chevron_right


Output:

  • Before clicking the button:
  • After clicking the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.