function init() {
// I'm createing a stand alone instance, you could also create a new class and extend EventEmitter.
var ee = new EventEmitter();
// This will house the previously clicked element.
var previousElement;
// I'm going to map the click events of multiple elements using regular expressions.
// First you have to define the possible events though.
// You could set up a function to prefix all of the IDs with "click-".
ee.defineEvents(['click-foo', 'click-bar', 'click-baz']);
// Now we map all click events through to the right event using this bit of magic. Man, I love this stuff.
// Using MooTools for this DOM stuff so it works everywhere. Mad4Milk, yo.
$$('body').addEvent('click', function(evt) {
ee.emitEvent('click-' + evt.target.get('id'), [evt.target]);
});
// Now we can add a listener for all those events using a regular expression!
ee.addListener(/click\-\w+/, function(target) {
if (previousElement) {
previousElement.removeClass('current');
}
target.addClass('current');
previousElement = target;
});
}
/*!
* EventEmitter v4.1.0 - git.io/ee
* Oliver Caldwell
* MIT license
* @preserve
*/
!function(r){"use strict";function t(){}function n(n,e){if(i)return e.indexOf(n);for(var t=e.length;t--;)if(e[t]===n)return t;return-1}var e=t.prototype,i=Array.prototype.indexOf?!0:!1;e._getEvents=function(){return this._events||(this._events={})},e.getListeners=function(n){var r,e,t=this._getEvents();if("object"==typeof n){r={};for(e in t)t.hasOwnProperty(e)&&n.test(e)&&(r[e]=t[e])}else r=t[n]||(t[n]=[]);return r},e.getListenersAsObject=function(n){var e,t=this.getListeners(n);return t instanceof Array&&(e={},e[n]=t),e||t},e.addListener=function(i,r){var e,t=this.getListenersAsObject(i);for(e in t)t.hasOwnProperty(e)&&-1===n(r,t[e])&&t[e].push(r);return this},e.on=e.addListener,e.defineEvent=function(e){return...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.