How to disable/enable an element with jQuery

You must have come across a situation where you have to make any element disable or enable. With jQuery, there are couple of ways using them you can enable/disable any element. jQuery really makes your task very easy. In below given example, I have disabled a textbox with id "txtName".

Approach 1

$("#txtName").attr("disabled", true);

Approach 2

$("#txtName").attr("disabled", "disabled");

If you are using jQuery 1.7 or higher version then use prop(), instead of attr().

$("#txtName").prop("disabled", "disabled");

If you wish to enable any element then you just have to do opposite of what you did to make it disable. However jQuery provides another way to remove any attribute.

Approach 1

$("#txtName").attr("disabled", false);

Approach 2

$("#txtName").attr("disabled", "");

Approach 3

$("#txtName").removeAttr("disabled");

Again, if you are using jQuery 1.7 or higher version then use prop(), instead of attr(). That's is. This is how you enable or disable any element using jQuery.

Also read:

Feel free to contact me for any help related to jQuery, I will gladly help you.



Responsive Menu
Add more content here...