Pokito
Pokito

Reputation: 33

Trigger "change" event on input does not work

I created kind of custom checkbox's plugin for my own project. So I wanted to trigger <input type="checkbox"> manually clicking on another button like this:

$("a#triggerButton").click(function(){
   $("input[type='checkbox']")
     .prop("checked", true)
     .triggerHandler("change");
});

Here is a typical example of what I'm doing: http://jsfiddle.net/fstqvq8k/3/ It seems that the event isn't triggered. I used a lot of methods but not seems to work too:

Upvotes: 1

Views: 132

Answers (2)

Mohd. Shaukat Ali
Mohd. Shaukat Ali

Reputation: 752

You can also trigger the checkbox event like..

$("input[type='checkbox']").click();

Upvotes: 0

Roko C. Buljan
Roko C. Buljan

Reputation: 205987

Since you dynamically create your button:

$("#container").on("click", "#triggerButton", function() {
    $("#customCheckbox")
        .prop("checked", true)
        .trigger("change");
});

http://jsfiddle.net/fstqvq8k/4/

Also from the Docs read:

The .triggerHandler() method does not cause the default behavior of an event to occur (such as a form submission).

Upvotes: 4

Related Questions