5

I create an event using hammer.js library like this:

Hammer(myElement).on("doubletap", function(evt){
            evt.preventDefault();
        });

How can I then remove the registred event? Can I also use Jquery?

Jacob
  • 3,580
  • 22
  • 82
  • 146
  • 2
    I have no experience of Hammer, but since the syntax looks very similar to jQuery, maybe Hammer uses `.off()` just like jQuery does? That is just a wild guess though. – Christofer Eliasson Mar 15 '13 at 08:29

2 Answers2

16

It's simply Hammer(myElement).off(eventName);

If you want to use jQuery, then the syntax is:

$(myElement).hammer().on(eventName, callback)

If you want to specify "namespace" for the event, then you declare eg.

$(myElement).hammer().on("tap.namespace", callback);
$(myElement).hammer().on("tap.anotherNamespace", callback2);

which makes it possible to detach only desired event, eg:

$(myElement).hammer().off("tap.anotherNamespace");
Samuli Hakoniemi
  • 18,740
  • 1
  • 61
  • 74
  • Unless I'm mistaken, Hammer does not store the callback, so you *have* to specify it. See also http://stackoverflow.com/questions/17367198/hammer-js-cant-remove-event-listener – Marc-André Lafortune Jul 11 '14 at 16:40
  • You have to save `Hammer(myElement)` to variable if you want to use `off()` method. http://stackoverflow.com/questions/39512670/hammer-js-off-method-doesnt-work/39513107#39513107 – RaV Sep 15 '16 at 14:17
1

In the documentation of hammer.js API, it is stated that it will not remove the dom events, using $(element).off() will not resolve your issues, a work around is to use the hammer.js jquery wrapper, their events are registered on an element using the bind function. Use the unbind to remove all or specific events.

$(element).hammer().unbind();

$(element).hammer().bind('swipeleft', 'swiperight', function(){});

The wrapper is here: https://github.com/hammerjs/jquery.hammer.js/blob/master/jquery.hammer.js

Pills
  • 13
  • 4