How to Disable and Enable Input in jQuery

Sheeraz Gul Feb 02, 2024
  1. jQuery Disable Input
  2. jQuery Enable Input
How to Disable and Enable Input in jQuery

To enable and disable the input field is an easy operation using jQuery. This tutorial demonstrates how to disable or enable the input field in jQuery.

jQuery Disable Input

The prop() method can disable an input using jQuery. The syntax for this method is given below.

prop('disabled', true)

It takes two parameters, the value disabled, which is set to true. Let’s try an example for the prop() method.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Disable Enable Input with jQuery</title>
    <style>
    label {
        display: block;
        margin: 10px 0;
    }
    </style>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script>
    $(document).ready(function(){
        $(".disable").click(function(){
            if($(this).prop("checked") == true){
                $('form input[type="text"]').prop("disabled", true);
            }
        });
    });
    </script>
</head>
<body>
    <form>
        <label><input type="checkbox" class="disable"> Check the box to disable the input fields below</label>
        <label>Name: <input type="text" name="username"></label>
        <label>ID: <input type="text" name="username"></label>
        <label>Address: <input type="text" name="username"></label>

    </form>
</body>
</html>

The code above will disable the input once we check the box. See output:

jQuery Disable Input

jQuery Enable Input

Similarly, the prop method is also used to enable the inputs in jQuery. The syntax to enable the input will be:

prop('disabled', false)

Where the parameter true will be changed to false, let’s try an example to enable the input.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Disable Enable Input with jQuery</title>
    <style>
    label {
        display: block;
        margin: 10px 0;
    }
    </style>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script>
    $(document).ready(function(){
        $('form input[type="text"]').prop("disabled", true);
        $(".disable").click(function(){
            if($(this).prop("checked") == true){
                $('form input[type="text"]').prop("disabled", false);
            }
        });
    });
    </script>
</head>
<body>
    <form>
        <label><input type="checkbox" class="disable"> Check the box to enable the input fields below</label>
        <label>Name: <input type="text" name="username"></label>
        <label>ID: <input type="text" name="username"></label>
        <label>Address: <input type="text" name="username"></label>

    </form>
</body>
</html>

The code above first disables all fields, then enables them by checking the box. See output:

jQuery Enable Input

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - jQuery Input