You can disable a text input element in HTML using JavaScript by setting its disabled
attribute to true
.
To do this, we write:
HTML:
<input type="text" id="myTextInput" value="Hello, world!">
<button onclick="disableInput()">Disable Input</button>
JavaScript:
function disableInput() {
var input = document.getElementById('myTextInput');
input.disabled = true;
}
In this example, we have an <input>
element with the ID 'myTextInput'
.
We have a button that, when clicked, calls the disableInput()
function.
Inside the disableInput()
function, we retrieve the input element using document.getElementById()
and then set its disabled
property to true
.
After calling disableInput()
, the text input will be disabled, and the user won’t be able to interact with it or modify its value.