bind mvc checkbox to javascript function

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
}

Answers:

Answer

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!!');
    }
});

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.