Skip to content

Instantly share code, notes, and snippets.

@rbarros
Created October 9, 2014 13:02
Show Gist options
  • Save rbarros/6c7888320ee0cdc3bab7 to your computer and use it in GitHub Desktop.
Save rbarros/6c7888320ee0cdc3bab7 to your computer and use it in GitHub Desktop.
EventListener
/*! EventListener - v1.0.0 - 2014-05-30
* Controla os eventos de click
* Copyright (c) 2014 Ramon Barros; Licensed MIT */
(function (root) {
'use strict';
var EventListener = function () {
this.version = '1.0.0';
};
EventListener.prototype.addEvent = function(el, type, fn) {
var i = 0;
if (root.document.addEventListener) {
if (el && el.nodeName || el === root) {
el.addEventListener(type, fn, false);
} else if (el && el.length) {
for (i; i < el.length; i++) {
root['EventListener'].addEvent(el[i], type, fn);
}
}
} else {
if (el && el.nodeName || el === root) {
el.document.body.attachEvent('on' + type, function () { return fn.call(el, root.event, root['EventListener']); });
} else if (el && el.length) {
for (i; i < el.length; i++) {
root['EventListener'].addEvent(el[i], type, fn);
}
}
}
};
root['EventListener'] = new EventListener();
}(this));
/*
EventListener.addEvent(document.getElementsByTagName('a'), 'click', function () {
});
EventListener.addEvent(document.getElementsByTagName('input'), 'submit', function () {
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment