jQuery Add Disabled Attribute to Input Element

Last Updated on September 17, 2022 by Roshan Parihar

In this tutorial, learn how jQuery add disabled attribute to the input element. The short answer is to use the attr() and pass disabled as its argument.

Making the input element disabled does not allow users to click and enter any data into the input box. Let’s find out with the different examples given below to add disabled attribute using jQuery.

Method 1: jQuery Add Disabled Attribute Using attr() Function

This method uses the attr() with two arguments required to add the disabled attribute to the input element. Pass "disabled" as the value of both the first and the second arguments as given below.

Output



The above example contains the input element and the button element. Initially, the input element is enabled to fill the data by the users. When you click the button given above, it makes the element disabled and does not allow users to enter any data on it.

Method 2: Using prop() Function of jQuery

In addition to the above example, you can also use the prop() function for making the input field disabled. The function takes the same two arguments as the value "disabled" that you have used in the above example. See the example given below to learn the method.

Output



You can enter your content in the input box given above. But, when you click the button given above, you will not be able to enter any data in the input box. With the click of the button, jQuery adds a disabled attribute to make it disabled.

You May Also Like to Read

Reference

Leave a comment

Your email address will not be published. Required fields are marked *

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