3

I want to dynamically disable/enable textbox aftre clicking the checkbox. How can I do it? I'm using this:

@{
    object addInput = (Model.AddInput) ? null : new { disabled = "disabled" };

 }

@Html.CheckBoxFor(model=> model.AddInput)

@Html.TextBoxFor(model => model.Input.Name, addInput)

but it works only on start. Clicking the checkbox isn't changing anything. How can do some binding to change disable state automaticaly?

0

3 Answers 3

8

This needs to be done in javascript.

@{
    object addInput = (Model.AddInput) ? null : new { disabled = "disabled" };

 }

@Html.CheckBoxFor(model=> model.AddInput)

@Html.TextBoxFor(model => model.Input.Name)


<script> 
    $('#AddInput').click(function() {
        var $this = $(this);

        if ($this.is(':checked')) {
            $('#Name').removeAttr("disabled"); 
        } else {
            $('#Name').attr("disabled", "disabled")
        }
    });
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It works, I only had to change from #Name to #Input_Name :)
0

Add jquery action on click on your checkbox and set state appropriately.

Comments

0

In case you want to enable it for some reason, you can also use the following:

      $('#Name').attr("disabled", false);

1 Comment

Question is about disabling - not enabling

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.