Categories
JavaScript Answers

How to disable an input type=text with JavaScript?

Spread the love

Sometimes, we want to disable an input type=text with JavaScript.

In this article, we’ll look at how to disable an input type=text with JavaScript.

How to disable an input type=text with JavaScript?

To disable an input type=text with JavaScript, we set the disabled property of the input to true.

For instance, we write

document.getElementById("foo").disabled = true;

to select the input with getElementById.

Then we set its disabled property to true to add the disabled attribute on the input.

Likewise, we write

document.getElementById("foo").readOnly = true;

to set the readOnly property to true to add the readonly attribute to the input.

Conclusion

To disable an input type=text with JavaScript, we set the disabled property of the input to true.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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