Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hammer.js object has no method addEventListener

I am getting the Error: Uncaught TypeError: Object [object Object] has no method 'addEventListener' hammer.js:168

my code is like this:

<script type="text/javascript" src="js/hammer.js"></script>

On device ready function:

 var resim = $('#kaydir');

 Hammer(resim).on('swipeleft', function(ev){
     console.log('left: ', ev);
 });

It seems the error is in hammer.js . What should I do?

like image 294
hdayi Avatar asked Jan 28 '15 11:01

hdayi


2 Answers

I imagine your issue is that you don't have Hammer.js's jQuery Plugin installed (GitHub).

Because of this, you cannot pass a jQuery object into the Hammer() function, your two options:

With the jQuery Plugin

Add the jQuery Plugin I've linked to above to your project, then call:

$('#kaydir').Hammer(...)

Without the jQuery Plugin

Pass only the element into Hammer() and not the jQuery object, by using [0]:

Hammer(resim[0]).on(...)

Or instead change your resim variable to hold the result of calling JavaScript's getElementById.

var resim = document.getElementById('kaydir');
Hammer(resim).on(...)
like image 99
James Donnelly Avatar answered Oct 23 '22 06:10

James Donnelly


If you are using jQuery, you should use the jQuery Hammer version and use it like that:

var resim = $("#kaydir");
resim.hammer().on("swipeleft", function(ev) {
    console.log('left: ', ev);
});
like image 2
flks Avatar answered Oct 23 '22 07:10

flks