How to add readonly attribute to an input tag in JavaScript ? Last Updated : 20 Dec, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Syntax: element.setAttribute( attributeName, attributeValue );Parameters: attributeName: It is a required parameter. It specifies the name of the attribute to be added. attributeValue: It is a required parameter. It specifies the value of the attribute to be added. Example 1: In this example, the read-only attribute of the form input text field is enabled by accessing the property. html <!DOCTYPE HTML> <html> <head> <title> Add a readonly attribute to an input tag </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p style = "font-size: 15px; font-weight: bold;"> The readonly attribute is added to input box on click on the button. </p> <form> Input : <input id = "Input" type="text" name="input_field" /> </form> <br> <button onclick = "GFG_Run()"> click here </button> <p id = "GFG_down" style = "color: green; font-size: 20px; font-weight: bold;"> </p> <script> function GFG_Run() { document.getElementById('Input').readOnly = true; document.getElementById("GFG_down").innerHTML = "Read-Only attribute enabled"; } </script> </body> </html> Output: OutputExample 2: In this example the read-only attribute of form input text field is enabled by using setAttribute() method . html <!DOCTYPE HTML> <html> <head> <title> Add a readonly attribute to an input tag </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p style = "font-size: 15px; font-weight: bold;"> The readonly attribute is added to input box on click on the button. </p> <form> Input : <input id = "Input" type="text" name="input_field" /> </form> <br> <button onclick = "GFG_Run()"> click here </button> <p id = "GFG_down" style = "color: green; font-size: 20px; font-weight: bold;"> </p> <script> function GFG_Run() { document.getElementById('Input').setAttribute('readonly', true); document.getElementById("GFG_down").innerHTML = "Read-Only attribute enabled"; } </script> </body> </html> Output: Output Comment More infoAdvertise with us Next Article How to add readonly attribute to an input tag in JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to use input readonly attribute in jQuery ? Use jQuery methods to add the readonly attribute to the form input field. jQuery attr() Method: This method set/return attributes and values of the selected elements. If this method is used to return the attribute value, it returns the value of the first selected element.If this method is used to se 3 min read How to Add an ID to Element in JavaScript ? In JavaScript, the ID is the unique identifier through which the elements can be identified and manipulated if required. We can add an ID to an element in JavaScript using various approaches that are explored in this article with practical demonstration. Below are the possible approaches: Table of C 2 min read How to Disable Input in JavaScript ? 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 programmatic 2 min read How to add/update an attribute to an HTML element using JavaScript? To add/update an attribute to an HTML element using JavaScript, we have multiple approaches. In this article, we are going to learn how to add/update an attribute to an HTML element using JavaScript. Below are the approaches used to add/update an attribute to an HTML element using JavaScript: Table 2 min read Set the Value of an Input Field in JavaScript We will learn how you can set the value of an input field in JavaScript. We will set user input to the placeholder value of the input field using JavaScript.Below are the approaches to set the value of an input field in JavaScript:Table of ContentBy using the innerHTML propertyBy using setAttribute 2 min read Like