How to make a text input non-editable using jQuery ?
An HTML document containing the input text area and the task is to make the text input non-editable with the help of jQuery. There are two approaches that are discussed below:
Approach 1: We are going to set the property readonly to true. To set the readonly property, we will use a prop() method.
- Example:
<!DOCTYPE HTML><html><head><title>How to make a text inputnon-editable using JQuery?</title><scriptsrc=</script></head><bodystyle="text-align:center;"><h1style="color:green;">GeeksForGeeks</h1><pid="GFG_UP"></p>Type Here: <inputid="input"/><br><br><buttononclick="GFG_Fun()">Click Here</button><pid="GFG_DOWN"></p><script>var el_up = document.getElementById('GFG_UP');var el_down = document.getElementById('GFG_DOWN');el_up.innerHTML = "Click on the button to "+ "perform the operation.";function GFG_Fun() {$("#input").prop("readonly", true);el_down.innerHTML ="Input element is now read-only";}</script></body></html> -
Output:
Approach 2: We are going to set the property readonly to true. In this example, we will use attr() method to set the property value.
- Example:
<!DOCTYPE HTML><html><head><title>How to make a text inputnon-editable using JQuery?</title><scriptsrc=</script></head><bodystyle="text-align:center;"><h1style="color:green;">GeeksForGeeks</h1><pid="GFG_UP"></p>Type Here: <inputid="input"/><br><br><buttononclick="GFG_Fun()">Click Here</button><pid="GFG_DOWN"></p><script>var el_up = document.getElementById('GFG_UP');var el_down = document.getElementById('GFG_DOWN');el_up.innerHTML = "Click on the button to "+ "perform the operation.";function GFG_Fun() {$("#input").attr("readonly", true);el_down.innerHTML ="Input element is now read-only";}</script></body></html> -
Output:


