1

I want to disable & enable a textbox with check /uncheck event of a checkbox using Jquery. The following code is not working for me. Am I missing anything?

Should I use onclick event or oncheck event of checkbox???

NB: I put alert statements & found that, if block alert is getting called multiple times, but else part is not getting called even a single time.

My CSHTML has : @Html.CheckBox("chkprivate", new { @onclick = "Private_Checked();" })

My JS code has :

function Private_Checked() {
    $("#chkprivate").click(function () {

        // if ($('#chkprivate').attr("Checked") == true) {
        if ($('#chkprivate').is(':checked')) {
            { $('#Customer_Name_Ext').attr("disabled", "disabled"); }
        }
        else {
            { $('#Customer_Name_Ext').removeAttr("disabled"); }
        }
    });
}

2 Answers 2

3

Try this

function Private_Checked() {
    $("#chkprivate").click(function () {
        $('#Customer_Name_Ext').attr("disabled", $(this).is(':checked'));
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

best one I've seen so far
2

try this

function Private_Checked(){

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

    if ($('#chkprivate').is(':checked')){
        $('#Customer_Name_Ext').attr("disabled", true);
    }
    else{
        $('#Customer_Name_Ext').removeAttr("disabled");
    }
});}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.