6

I have started using Hammer.js (https://github.com/eightmedia/hammer.js) which is a great little script, but I'm unsure how to delegate the events using jQuery's .on() event handler.

I've set up a little jsfiddle example here: http://jsfiddle.net/will/3dUKu/1/

var i = 0;
// How would I apply hammer to this situation?
$('nav').on('click', 'button', function() {
    $('<button id="' + i + '">Extra button ' + i + ' (' + $(this).attr('id') + ')</button>').appendTo('nav');
    i++;
});

1 Answer 1

3

Don't know if you ever found an answer to your question. The solution is simple and two part. First, since you are trying to use the calls with jQuery you should use the jQuery plugin version of Hammer. A current version of the jQuery plugin can be found at:

http://eightmedia.github.io/hammer.js/dist/jquery.hammer.min.js

The second part is you need to use Hammer's method with the object selection. You do this by inserting the Hammer call between the selector and the binding. Like so:

jQuery object:

$('nav').on('click', 'button', function(){ 
    /* ... */ 
});

jQuery Hammer object:

$('nav').hammer().on('click', 'button', function(){
    /* ... */
});

That's it...

2
  • this doesn't work for me with latest hammerjs.. $('.parent').hammer().on('tap', '.child', function() { console.log('tap'); });
    – Rick Li
    Commented Sep 19, 2014 at 9:07
  • All the jQ plugin does it instantiate a Hammer instance and attach it to the element. It doesn't look like it actually helps with event delegation at all.
    – Lambart
    Commented Apr 7, 2015 at 18:04

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