cycling jQuery_Succinctly | Page 71

You can attach unlimited handlers to a single DOM element using jQuery. jQuery provides the one() event handling method to conveniently bind an event to DOM elements that will be executed once and then removed. The one() method is just a wrapper for bind() and unbind(). Programmatically invoke a specific handler via short event methods The shortcut syntax—e.g. .click(), mouseout(), and focus()—for binding an event handler to a DOM element can also be used to invoke handlers programmatically. To do this, simply use the shortcut event method without passing it a function. In theory, this means that we can bind a handler to a DOM element and then immediately invoke that handler. Below, I demonstrate this via the click() event. Sample: sample66.html Say Hi Say Hi Notes: It is also possible to use the event trigger() method to invoke specific handlers—e.g. jQuery('a').click(function(){ alert('hi') }).trigger('click'). This will also work with namespaced and custom events. jQuery normalizes the event object jQuery normalizes the event object according to W3C standards. This means that when the event object is passed to a function handler, you do not have to worry about browser-specific implementations of the event object (e.g. Internet Explorer’s window.event). You can use the following attributes and methods of the event object worry-free from browser differences because jQuery normalizes the event object. 71