Set a text field to read-only:
document.getElementById("myText").readOnly = true;
Click the button to set the text field to read-only.
<!DOCTYPE html> <html> <body> Name: <input type="text" id="myText"> <button onclick="myFunction()">Test</button> <script> function myFunction() {//from w w w .ja v a 2 s .c o m document.getElementById("myText").readOnly = true; } </script> </body> </html>
The readOnly property sets or gets whether a text field is read-only.
A read-only field cannot be modified.
Value | Description |
---|---|
true | The text field is read-only |
false | Default. The text field is changeable |
The readOnly property returns true if the text field is read-only, otherwise it returns false.