Skip to content

Instantly share code, notes, and snippets.

@dciccale
Last active December 17, 2015 00:29

Revisions

  1. dciccale revised this gist Aug 13, 2013. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions demo.html
    Original 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');
    var callback = function () { alert('Button clicked!') };

    // callback function
    var callback = function () { alert('Button clicked!'); };

    // alert 'Button clicked!' when clicked
    addEvent(element, 'click', callback);

    // remove the event
    // remove the 'click' event
    removeEvent(element, 'click', callback);
    </script>
  2. dciccale created this gist May 5, 2013.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # removeEvent

    Cross-browser function to remove events from DOM elements.
    18 changes: 18 additions & 0 deletions demo.html
    Original 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>
    12 changes: 12 additions & 0 deletions removeevent.js
    Original 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);
    }
    };
    1 change: 1 addition & 0 deletions removeevent.min.js
    Original 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)}}