2

Trying to bind an mvc helper checkbox with jquery to a javascript function and that function will do something based on the fact that it is checked or not.

PART 1: How do you successfully bind the checkbox to a click or change event?

PART 2: How do you check the checkbox for checked or not?

<div class="editor-label">
    @Html.LabelFor(model => model.Immediate)
</div>
<div class="editor-field">
    @Html.CheckBoxFor(model => model.Immediate, new {id = "chkImmediate" })
</div>

My attempt at binding:

$("#chkImmediate").click(clickEvent());

function clickEvent() {
        alert($("#chkImmediate").checked); //want it to show true/false
}
2
  • Can you please include the entire contents of the <script> the above JavaScript is in. Commented Apr 18, 2014 at 22:51
  • 1
    use change event not click event Commented Apr 19, 2014 at 5:40

1 Answer 1

6

Assumming the id of the checkbox is correct, you can bind the event using jQuery like this:

$("#chkImmediate").change(function() {
    if(this.checked) {
        alert('It's checked!!');
    } else {
        alert('It's not checked!!');
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

This got me close. Thanks. What I ended up doing was this $(function () { $('#chkImmediate').change(function () { var checked = $(this).is(':checked'); clickEvent(checked); }); });

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.