Skip to Main Content
jQuery Pocket Reference
book

jQuery Pocket Reference

by David Flanagan
December 2010
Intermediate to advanced content levelIntermediate to advanced
160 pages
3h 31m
English
O'Reilly Media, Inc.
Content preview from jQuery Pocket Reference

Triggering Events

The event handlers you register are automatically invoked when the user uses the mouse or keyboard, or when other kinds of events occur. Sometimes, however, it is useful to be able to trigger events manually. The simple way to do this is to invoke one of the event registration methods (like click() or mouseover()) with no argument. Just as many jQuery methods serve as both getters and setters, these event methods register an event handler when invoked with an argument, and trigger event handlers when invoked with no arguments. For example:

// Act as if the user clicked the Submit button
$("#my_form").submit();    

The submit() method in the line above synthesizes an Event object and triggers any event handlers that have been registered for the “submit” event. If none of those event handlers returns false or calls the preventDefault() method of the Event object, the form will actually be submitted. Note that events that bubble will do so even when triggered manually. This means that triggering an event on a selected set of elements may also trigger handlers on the ancestors of those elements.

It is important to note that jQuery’s event triggering methods will trigger any handlers registered with jQuery’s event registration methods, and they will also trigger handlers defined on HTML attributes or Element properties such as onsubmit. But you cannot manually trigger event handlers registered with addEventListener() or attachEvent() (those handlers will still be invoked ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

jQuery and JavaScript Phrasebook

jQuery and JavaScript Phrasebook

Brad Dayley
Beyond jQuery

Beyond jQuery

Ray Nicholus
Head First jQuery

Head First jQuery

Ryan Benedetti, Ronan Cranley

Publisher Resources

ISBN: 9781449398958Supplemental ContentErrata Page