How to disable an input type=text?

kawtousse picture kawtousse · May 20, 2010 · Viewed 361k times · Source

I want to disable writing in an input field of type text using JavaScript, if possible. The input field is populated from a database; that is why I don't want the user to modify its value.

Answer

hudolejev picture hudolejev · May 20, 2010
document.getElementById('foo').disabled = true;

or

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

Note that readOnly should be in camelCase to work correctly in Firefox (magic).

Demo: https://jsfiddle.net/L96svw3c/ -- somewhat explains the difference between disabled and readOnly.