This post will discuss how to disable and enable an input text box in JavaScript and jQuery.

When the element is disabled, it cannot accept clicks. To disable an input element, its disabled HTML attribute should be false.

1. Using jQuery

To set the input box disabled property to true or false with jQuery, you can use the .prop() function.

jQuery


HTML



Edit in JSFiddle

 
We can simplify the code to:

Edit in JSFiddle

 
Alternatively, you can use the .attr() method to set the disabled attribute of the input element and .removeAttr() to remove the attribute from the input element.

jQuery


HTML



Edit in JSFiddle

2. Using JavaScript

With plain JavaScript, you can use the disabled property of the actual DOM object to enable or disable the input.

JS


HTML



Edit in JSFiddle

That’s all about enabling and disabling an input text box in JavaScript and jQuery.