Skip to content

Instantly share code, notes, and snippets.

@midned
Last active December 19, 2015 03:08
Show Gist options
  • Save midned/5887752 to your computer and use it in GitHub Desktop.
Save midned/5887752 to your computer and use it in GitHub Desktop.
Event listening with #swag
Node.prototype.on = Node.prototype.addEventListener;
Node.prototype.off = Node.prototype.removeEventListener;
var elem = document.getElementById('some-element');
function yolo(){
console.log('yolo');
}
elem.on('click', yolo);
elem.off('click', yolo);
// So we don't need to iterate over each element
NodeList.prototype.on = function()
{
var elem, i;
i = 0;
while (i < this.length)
{
elem = this.item(i);
elem.on.apply(elem, arguments);
++i;
}
};
NodeList.prototype.off = function()
{
var elem, i;
i = 0;
while (i < this.length)
{
elem = this.item(i);
elem.off.apply(elem, arguments);
++i;
}
};
var divs = document.getElementsByTagName('div');
divs.on('click', yolo);
divs.off('click', yolo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment