Categories
JavaScript Answers

How to disable an input type=text with JavaScript?

Spread the love

To disable an input type=text with JavaScript, we set the disabled or readOnly property.

For instance, we write

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

or

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

to get the input with getElementById.

Then we disable the input by setting the disabled or readOnly property to true.

readOnly keeps the original input style when disabled.

disabled grays out the input when disabled.

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 *