g(..).addEvent(eventName, handlerFn, [useCapture])

Adds the event listener/handler, handlerFn, for the specified event, eventName, to each element in the Gimme Object's internal array

Returns: Itself (the same Gimme Object is returned so additional methods can be chained together seamlessly).
  • eventName, required: The string name of the event for which the listener/handler function will "listen." This should not include the prefix "on."
  • handlerFn, required: A reference to the function to be invoked when the event specified by eventName is dispatched.
  • useCapture, optional (default = false): Specified whether or not to use event capture (currently only supported in W3C browsers).

// adds a click event handler, sayHello, when any <div> element on the page is clicked
g('div').addEvent('click', sayHello);

// adds a mouseover event handler, fn1, and a mouseout event handler, fn2, when any <p> element is moused over or moused out
g('p').addEvent('mouseover', fn1).addEvent('mouseout', fn2);

// adds an anonymous mousedown event handler to all <img> elements that have that the class "special" and which are direct children of <p> elements
g('p > img.special').addEvent('mousedown', function() { alert('A special image!'); });

Related Information:
Any time an event is wired up using .addEvent(..), the handler function is automatically passed an Event object that can be used to obtain additional information about the event that has just been dispatched. Gimme follows the W3C model very closely, so you can safely reference event properties such as .target, .relatedTarget, .preventDefault(), and .stopPropagation(). Additionally, the 'this' keyword is properly maintained in all browsers (including IE).

g('a').addEvent('click', testFn);

function testFn(e)
     e.preventDefault();			// prevent the default action of the <a> element that was clicked
     e.stopPropagation();			// do not allow the event to propagate

     alert(this.innerHTML);			// alert the .innerHTML of the <a> element that was clicked

Last edited Jun 7, 2007 at 6:17 PM by sstchur, version 8


No comments yet.