jQuery simulate a toggle event

Sam Deering
Share

Using jQuery to “trigger” simulate a toggle event you can simply trigger a click event like this:

.trigger('click');

Then say if you have 2 buttons which do exactly the same thing you can simulate a click on the button with the event handler attached to promote code reuse.

//simulate an event
$(this).parents('.parent').find('.controls cancel').trigger('click');

The HTML