How to Disable Input in JavaScript ? Last Updated : 07 Aug, 2024 Comments Improve Suggest changes Like Article Like Report Disabling input fields in JavaScript is a common task when you want to restrict users from interacting with specific form elements. This can be useful in various scenarios, such as preventing modifications to fields that are conditionally locked or ensuring certain inputs are controlled programmatically.We can use various methods and attributes to disable the input.Table of ContentUsing disabled attributeUsing readOnly attributeUsing disabled attributeThe disabled attribute completely restricts users from interacting with the input elements. When an input field is disabled, it is rendered in an uneditable form and does not accept any input from the users. Additionally, disabled fields are not submitted with the form data.Syntax:element.disabled = true;Example: The below example uses a disabled attribute to disable input in JavaScript. HTML <!DOCTYPE html> <html> <head> <title>Using disabled attribute</title> <style> h1 { color: green; } </style> </head> <body> <h1>GeeksforGeeks</h1> <h3>Using disabled attribute</h3> <input type="text" id="input1" value="GeeksforGeeks"> <script> document.getElementById("input1"). disabled = true; </script> </body> </html> Output:Using readOnly attributeThe readOnly attribute makes the input field uneditable while visually indicating that it is in a read-only state. Unlike disabled, the readOnly attribute allows the input field to be included in form submissions. This is useful when you want to display data that should not be modified by the user but still needs to be part of the form submission.Syntax:element.readOnly = true;Example: The below example uses the readOnly attribute to disable input in JavaScript. HTML <!DOCTYPE html> <html> <head> <title>Using readOnly attribute</title> <style> h1 { color: green; } </style> </head> <body> <h1>GeeksforGeeks</h1> <h3>Using readOnly attribute</h3> <input type="text" id="input2" value="GeeksforGeeks"> <script> document.getElementById("input2"). readOnly = true; </script> </body> </html> Output:By understanding and using the disabled and readOnly attributes appropriately, you can effectively control user interaction with form elements based on your application's requirements. Comment More infoAdvertise with us Next Article How to Disable Input in JavaScript ? G gauravgandal Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Disable Ctrl + C in JavaScript ? Disabling Ctrl+C in JavaScript involves intercepting the keyboard event and preventing the default action associated with the combination. There are several approaches to disable Ctrl+C in JavaScript which are as follows: Table of Content Using Event ListenersModifying the clipboard eventUsing Event 2 min read How to Disable Ctrl+V (Paste) in JavaScript? What is Ctrl + V ?The ctrl+V is a keyboard shortcut used to paste anything from anywhere. It can be disabled for a particular task or page. Let's see how to disable cut, copy, paste, and right-click. To disable the ctrl+V (paste) keyboard shortcut in JavaScript, you would typically capture the keydo 3 min read How to Disable Textarea using JavaScript? This article will show you how to disable the textarea using JavaScript. Disabling a textarea using JavaScript can be done by setting the disabled property of the textarea element to true. This prevents the user from editing the content of the textarea. There are some reasons to use disabled textare 2 min read How to add readonly attribute to an input tag in JavaScript ? Use the setAttribute() Method to add the read-only attribute to the form input field using JavaScript. setAttribute() Method This method adds the defined attribute to an element and gives it the defined value. If the specified attribute is already present, then the value is being set or changed. Syn 2 min read How to Disable a Button Conditionally in JavaScript? In JavaScript, you can conditionally disable buttons based on specific criteria to control user interactions. Using simple if-else statements or event listeners, you can dynamically enable or disable buttons depending on form validation, user actions, or other conditions. This helps regulate the ava 3 min read How to disable input type text area? To disable an input type=text by using the input disabled attribute. A disabled input is un-clickable and unusable. It is a Boolean attribute. The disabled elements are not submitted in the form. Syntax: <input type="text" disabled>Example 1: To disable an input field of type "text" by using t 2 min read How to disable radio button using JavaScript ? Radio buttons let users select one option from many, commonly used in forms. Sometimes, based on a user's previous choice, certain options need to be disabled. For example, if a user selects "No" to playing games, we can disable related game options. Using JavaScript, about 65% of forms use such log 4 min read How to disable arrow key in textarea using JavaScript ? Given an HTML element containing the <textarea> element and the task is to disable scrolling through arrow keys with the help of JavaScript. Approach 1: Add an event listener onkeydown on the window.If the event happens then check if the keys are arrow or not.If arrow key is pressed then preve 3 min read HTML DOM Input Tel disabled Property The DOM input tel disabled Property is used to set or return whether the input tel field must be disabled or not. A disabled tel field is un-clickable and unusable. It is a boolean attribute that reflects the disabled attribute. It is usually rendered in grey color by default in all the browsers. S 2 min read HTML | DOM Input Text disabled Property The DOM Input Text disabled Property is used to set or return the whether the Input Text Field must be disabled or not. A disabled Text Field is un-clickable and unusable. It is a boolean attribute and used to reflect the HTML Disabled attribute. It is usually rendered in grey color by default in al 2 min read Like