Suresh Pattu
Suresh Pattu

Reputation: 6209

How to bind click and change event for checkbox

I am adding some checkbox to the page through ajax how to bind the click and change function for these checkboxes my script is here

$(document).delegate("#filterOptions input[name=inNeedAmountRange]").bind("click change", function () {
    var elementValue = $(this).val();
    if ($(this).is(':checked')) {
        alert('checked : ' + elementValue);
    }
} else {
    alert('Not checked :' + elementValue);
}
});

Upvotes: 2

Views: 10140

Answers (2)

Try .on()

Fiddle Demo

$(document).on("click change", "#filterOptions input[name=inNeedAmountRange]", function () {
    var elementValue = this.value;
    if ($(this).is(':checked')) {
        alert('checked : ' + elementValue);
    } else {
  // ^ remove extra }
        alert('Not checked :' + elementValue);
    }
});

Event Delegation

Upvotes: 14

Ramesh
Ramesh

Reputation: 4293

Try: http://api.jquery.com/on/

$(document).on("change click", ""#filterOptions input[name=inNeedAmountRange]", function() {
    //do something
});

You can replace document with any static container.

Upvotes: 3

Related Questions