internally1
internally1

Reputation: 97

disable a inputfield on clicking a checkbox

i want to disable an input field (id = reg) on clicking the checkbox. I want to use jquery, but I did it with no luck.

Here is my code:

<head>   
    <meta charset="utf-8">   
    <title>FIMS</title>   
    <meta name="description" content="Sales tracking system">   
    <title>FIMS::Data analyst</title>
    <link rel="stylesheet" href="../bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="../includes/my_style_sheet/my_style.css">
    <link rel="stylesheet" href="../includes/my_style_sheet/mys_style_sheet.css">
    <link rel="stylesheet" href="../includes/my_js/smoothness/jquery-ui-1.9.1.custom.css">
    <script type="text/javascript" src="../includes/my_js/js_library.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('#all').change(function(){
            $('#reg').attr("disabled","disabled");
        });
    });
    </script>
</head>

HTML code

<div class="control-group">
    <label class="control-label" for="id_password">Choose District</label>
    <div class="controls">
        <select class="bootstrap-select" name="district" id="dis" disabled='disabled'></select>
    </div>
</div>

<div class="control-group">
    <label class="control-label" for="id_password">Regionwise</label>
    <div class="controls">
        <label class="checkbox">
            <input type="checkbox" name="all" id="all">
        </label>
    </div>
</div>                   

Upvotes: 0

Views: 168

Answers (4)

Vivek Jain
Vivek Jain

Reputation: 3889

You may try the following code:

$(document).ready(function() {
    $('#all').toggle(function() {
        if($('#all').attr('checked')) {
            $('#reg').attr("disabled", true);
        } else {
            $('#reg').attr("disabled", false);
        }
    });
});

You may also use the click() event of the checkbox.

Please use this as a starting point and not as a copy-paste solution.

Upvotes: 0

Nagesh Salunke
Nagesh Salunke

Reputation: 1288

It is Simple

Try :

$("#checkbox").click(function(){

    $("#sample").attr("disabled","true");
});

Here is Jsfiddle Link : http://jsfiddle.net/88px7/1/

Upvotes: 1

Yogesh
Yogesh

Reputation: 150

what is id of checkbox ?

your code looks right but please check http://api.jquery.com/change/

check without $(document).ready() also check your jquery reference

Upvotes: 0

palaѕн
palaѕн

Reputation: 73896

You can do this:

$(document).ready(function () {
    $('#all').change(function () {
        $('#reg').prop("disabled", this.checked);
    });
});

FIDDLE DEMO

Upvotes: 3

Related Questions