Sometimes, we need to disable textbox or input fields using JavaScript. To enable/disable textbox in JavaScript, we can use DOM properties.
It is quite easy to disable an input field using JavaScript. Here, we are going to disable an input field onclick event of a button using simple js. However, you can set it to any event like onhover, ondoubleclick, onchange, etc. It may depend on your webpage or form user flow.
Example 1
<!DOCTYPE html>
<html>
<body>
<input type="text" id="my_field" name="Name" placeholder="Enter Your Name"/>
<br>
<button >
As we can see in the above example, we have to select the input field we want to disable first. After that, we have to set its disabled property to true.
We can also remove the disabled property of the input field using JavaScript. In the below example, we will learn how to remove disabled property and unlock the input field to the user.
Read Also: Change Text Onclick JavaScript
Example 2
<!DOCTYPE html>
<html>
<body>
<input type="text" id="my_field" name="Name" placeholder="Enter Your Name"/>
<br>
<button >
In the above example, we created another button to enable the input field. To enable we set the disabled property of the input field to the “”(empty) value which will enable the input field.
I hope you know how to enable and disable textbox or input field using JavaScript.
Happy Scripting 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…