Reputation: 33
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:
$("input[type='checkbox']").change();
$(document).on("change", "input[type='checkbox']", function(){});
Upvotes: 1
Views: 132
Reputation: 752
You can also trigger the checkbox event like..
$("input[type='checkbox']").click();
Upvotes: 0
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