Hammer.js with the JQuery plugin not firing event

510 Views Asked by At

I have the following JS for a doubletap event on the element '.snap_img' with the Hammer.js JQuery plugin.

$("body").hammer().on('doubletap','.snap_img',function() {
    img_src = (this).getAttribute('src');
    alert(img_src);
    return false;
});

The element '.snap_img' is created dynamically by an AJAX post() request. Therefore, I need event delegation to be able to fire the Hammer event. This JS is based on this piece of code which successfully fires a click event on the dynamically loaded .snap_img elements:

$("body").on('click','.snap_img',function() {
    img_src = (this).getAttribute('src');
    alert(img_src);
    return false;
});

How do I make the code with the Hammer JQuery plugin work? I suppose the solution should be very similar to the second piece of code in this question, but I can't seem to figure it out.

1

There are 1 best solutions below

1
On BEST ANSWER

If you are using Hammer, you can try something like this:

var i = 0
$('.snap_img').get(0).hammer().on('doubletap', function(e) {
  i++
  console.log( 'double tap' + i )
})