How to use readOnly attribute In Javascript
In this approach, we are using the JavaScript readOnly property to set the readOnly attribute to the input field. This makes the input field uneditable while visually indicating that it is in a read-only state and cannot be editable.
Syntax:
element.readOnly = true;
Example: The below example uses the readOnly attribute to disable input in JavaScript.
HTML
<!DOCTYPE html> < head > < title >Example 2</ title > < style > h1 { color: green; } </ style > </ head > < body > < h1 >w3wiki</ h1 > < h3 >Using readOnly attribute</ h3 > < input type = "text" id = "input2" value = "w3wiki" > < script > document.getElementById("input2"). readOnly = true; </ script > </ body > </ html > |
Output:
How to Disable Input in JavaScript ?
In JavaScript, disabling the input field mainly restricts the users from interacting with the input elements, and also prevents the modifications to the form elements such as buttons or input fields.
We can use various methods and attributes to disable the input.
Table of Content
- Using disabled attribute
- Using readOnly attribute
Contact Us