introduction
downloadGator is a small (~0.8 kb
minified + gzipped), simple, standalone, event delegation library.
If you are familiar with jQuery then it may look familiar to you.
Gator(document).on('click', '.expand', function(e) {
e.preventDefault();
_doSomething(this);
});
I am just one person so anything you can donate to help support development will help.
browser support
Out of the box gator works in Chrome, Safari 5+, Firefox 3.6+, and Internet Explorer 9+. The legacy.js
plugin adds support for Safari < 5, and Firefox < 3.6 and Internet Explorer 6, 7, and 8.
why use event delegation? +
There are already a number of articles about event delegation and the advantages over traditional event handling. Here are just a few:
-
improved performance/memory usage
If you have a table with hundreds of cells, and you want to do something when the user clicks on a cell, without delegation you would have to bind the event to each cell. With delegation you could bind a single click handler to the
table
element. -
no need to re-attach events
If you use AJAX to dynamically insert/update content on the current page then you don't have to worry about removing/adding the events every time the content changes. You can bind the events once to a higher level element.
-
fewer functions to manage
Without delegation you would have to manage a lot of callback functions. If you remove a bunch of elements you would have to make sure to remove all the events associated with them.
documentation +
-
creating a gator instance
The methods that do event delegation are prototype methods on the
Gator
object. To instantiate a Gator object call the function directly:Gator(element);
or use the
new
keywordnew Gator(element);
-
attaching events
Attaching events uses the
Gator.on
method. Theon
method accepts 3 arguments:- eventType
- Any browser event that you would normally pass to
addEventListener
.You can also pass in an array of multiple event types, for example
['mousedown', 'mouseup']
- selector
- A CSS selector for elements to look for within the outer element*.
- callback
- A callback function to fire when the event occurs. The callback gets passed a single argument containing the browser Event object. The callback is invoked in the context of the selector passed in. That means in the example above if you reference
this
in your callbackthis
corresponds to thetd
element.
*Note that when using the legacy plugin you can only use a single class name, id, or tag name for the selector argument.
Suppose you wanted to add a click event to every
td
inside of atable
var table = document.getElementsByTagName('table')[0]; Gator(table).on('click', 'td', _doStuff);
This example will fire even if you click on an element inside of a
td
. For example if you click on aspan
inside of atd
it will trigger the event as if you clicked on thetd
andthis
in the callback will still reference thetd
. You can usee.target
to see what element was actually clicked on.As a convenience you can leave out the selector argument to bind an event directly to the original element
Gator(document).on('click', function(e) { console.log('clicked on document'); })
If you bind multiple callbacks to the same event/element/selector group they will be fired in the order they were bound.
No matter how many callbacks you bind to an element in Gator only one event listener will be attached to the actual DOM element. Take the code on this page for example
Gator(document).on('click', '.expand', _handleExpandClick); Gator(document).on('click', 'pre', _selectCode); Gator(document).on('click', function() { selected = false; });
Even though there are 3 click handlers
document.addEventListener('click'...
is only called once. -
removing events
Removing events uses the
Gator.off
method. This method accepts the same arguments asGator.on
.In Javascript when you want to remove an event you have to pass a reference to the original callback you passed to the event. Gator keeps track of that internally so in most cases you don't have to worry about it.
Let's say you have the following events set up
var gator = new Gator(document); gator.on('mousedown', _documentDown); gator.on('mousedown', 'ul', _ulDown); gator.on('mousedown', 'li', _liDown); gator.on('mousedown', 'li', _otherLiDown);
If you want to remove all events from
document
Gator(document).off();
If you want to remove all
mousedown
eventsGator(document).off('mousedown');
If you want to remove all
mousedown
events onli
elementsGator(document).off('mousedown', 'li');
If you want to remove a single
mousedown
event onli
elements pass a reference to the original callbackGator(document).off('mousedown', 'li', _otherLiDown);
-
event bubbling
When using event delegation there is not really a concept of event bubbling because the event has already reached a higher level element by the time it is captured.
That being said, Gator internally mimics traditional events bubbling. This allows you to use
e.preventDefault()
ande.stopPropagation()
as you normally would in your callback functions.As an added convenience if you return
false
in your callback it is a shortcut for callinge.preventDefault()
followed bye.stopPropagation()
.Using the previous example if you wanted to stop the mousedown event from getting back to the
document
you could do this in yourul
handlervar _ulDown = function(e) { e.stopPropagation(); };
contributing +
If you want to contribute or submit feature requests/bug reports fork the project on GitHub.
If you want to contribute to my wallet to help support development use Gittip.