Triggering Event Handlers
Prev Chapter 5. Events Next

Triggering Event Handlers

jQuery provides a way to trigger the event handlers bound to an element without any user interaction via the $.fn.trigger method. While this method has its uses, it should not be used simply to call a function that was bound as a click handler. Instead, you should store the function you want to call in a variable, and pass the variable name when you do your binding. Then, you can call the function itself whenever you want, without the need for $.fn.trigger.

Example 5.9. Triggering an event handler the right way

var foo = function(e) { 
    if (e) { 
        console.log(e); 
    } else {
        console.log('this didn\'t come from an event!');
    }
};


$('p').click(foo); 

foo(); // instead of $('p').trigger('click')


Copyright Rebecca Murphey, released under the Creative Commons Attribution-Share Alike 3.0 United States license.


Prev Up Next
Inside the Event Handling Function Home Increasing Performance with Event Delegation