Last active
December 17, 2015 00:29
Revisions
-
dciccale revised this gist
Aug 13, 2013 . 1 changed file with 8 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,13 +6,19 @@ <script> // use addEvent cross-browser shim: https://gist.github.com/dciccale/5394590/ var addEvent = function(a,b,c){try{a.addEventListener(b,c,!1)}catch(d){a.attachEvent('on'+b,c)}}; // removeEvent var removeEvent = function(a,b,c){try{a.removeEventListener(b,c,!1)}catch(d){a.detachEvent('on'+b,c)}}; // DOM element var element = document.getElementById('btn'); // callback function var callback = function () { alert('Button clicked!'); }; // alert 'Button clicked!' when clicked addEvent(element, 'click', callback); // remove the 'click' event removeEvent(element, 'click', callback); </script> -
dciccale created this gist
May 5, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ # removeEvent Cross-browser function to remove events from DOM elements. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ <!doctype html> <title>Demo</title> <span id="btn">Button</span> <script> // use addEvent cross-browser shim: https://gist.github.com/dciccale/5394590/ var addEvent = function(a,b,c){try{a.addEventListener(b,c,!1)}catch(d){a.attachEvent('on'+b,c)}}; var removeEvent = function(a,b,c){try{a.removeEventListener(b,c,!1)}catch(d){a.detachEvent('on'+b,c)}}; var element = document.getElementById('btn'); var callback = function () { alert('Button clicked!') }; // alert 'Button clicked!' when clicked addEvent(element, 'click', callback); // remove the event removeEvent(element, 'click', callback); </script> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ /** * @param element The element to remove the event from * @param type Event type (i.e. 'click') * @param callback Original function that was attached to the specified event */ function (element, type, callback) { try { element.removeEventListener(type, calback, false); } catch (e) { element.detachEvent('on' + type, callback); } }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ function(a,b,c){try{a.removeEventListener(b,c,!1)}catch(d){a.detachEvent('on'+b,c)}}