1

I'm using Hammer.js and I've included Hammer and the Hammer JQuery plugin like this:

<script src='res/stat/js/hammer.js'></script>
<script src='res/stat/js/jquery.hammer.js'></script>

Now, with this code I'm trying to get double tap to work:

<script>
    var element = document.getElementsByClassName('snap_item');
    var hammertime = Hammer(element).on("doubletap", function(event) {
        alert('hello!');
    });   
</script>

The element with classname 'snap_item' exists and is spelled correctly. The path to Hammer.js and the JQ plugin are also correct. I've tested it on the iPhone 5S and it doesn't do anything when I double tap the element.

Does anyone know what's going wrong here? I have JQuery V2 included as well. Is that causing the problem?

0

1 Answer 1

2

document.getElementsByClassName returns an array of elements. The Hammer constructor can only take one element. Initialize each on it's own:

var elements = document.getElementsByClassName('snap_item');

[].slice.call(elements).forEach(function(element) {
    var hammertime = new Hammer(element);
    hammertime.on('doubletap', function(event) {
         alert('hello!');
    });
});

JSFiddle Demo: http://jsfiddle.net/nw6dvgck/

4
  • For some reason it doesn't work. On JSFiddle it works, but when I copy the exact same code, it doesn't work.
    – erol_smsr
    Commented Oct 10, 2014 at 18:23
  • Are you using the latest hammer.js? I didn't include jQuery hammer. Commented Oct 10, 2014 at 18:32
  • Yeah, it's the latest Hammer.js
    – erol_smsr
    Commented Oct 10, 2014 at 23:26
  • For me works in jsfiddle. I don't understand whole example but thanks
    – Ludus H
    Commented Jul 22, 2017 at 17:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.