How to disable a text input with JavaScript?

Spread the love

You can disable a text input element in HTML using JavaScript by setting its disabled attribute to true.


To do this, we write: HTML:



JavaScript:
function disableInput() { var input = document.getElementById('myTextInput'); input.disabled = true;
}

In this example, we have an 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.