Dom-event

dom-event

Add/remove DOM events

Sweet and simple module that helps you to attach and remove events to a DOM element with ease and cross browser compliance all in just 15 lines of code ;)

Add event:

1
2
3
4
5
function on (element, event, callback, capture) {
  !element.addEventListener && (event = 'on' + event);
  (element.addEventListener || element.attachEvent)(event, callback, capture);
  return callback;
}

Remove event:

1
2
3
4
5
function off (element, event, callback, capture) {
  !element.removeEventListener && (event = 'on' + event);
  (element.removeEventListener || element.detachEvent)(event, callback, capture);
  return callback;
}

Install it: npm install dom-event

Sample usage:

1
2
3
4
5
6
7
8
var on = require('dom-event');
var off = on.off; // :)

on(document.body, "click", hello);

function hello() {alert("hello");}

off(document.body, "click", hello); // No alerts :)

GIF FTW!

dom-event

P.S: All Web realted modules would need help from browserify or related tools to run on the web.

Suggest a module

Comments