i'm creating A webapp for mobile devices and I am using ontouchend for most but not all 'clicks' The problem is I would like to disable the onclick event on my ontouchend functions. Any ideas? Thanks
i'm creating A webapp for mobile devices and I am using ontouchend for most but not all 'clicks' The problem is I would like to disable the onclick event on my ontouchend functions. Any ideas? Thanks
Share Improve this question asked Feb 10, 2012 at 0:05 helptomouthelptomout 1954 silver badges9 bronze badges 1- You must put more research effort in your questions, like code for example. – Gigi Commented Feb 10, 2012 at 1:30
2 Answers
Reset to default 4For mobile devices click event is switched with touchend event. With JQuery you can add multiple events using .on() but if you add both a touch and click event both will fire leading to multiple triggers. It is better to use a switch like below:
var eventType = (/Android|webOS|iPhone|iPad|iPod|BlackBerry|Windows Phone/i.test(navigator.userAgent) ) ? 'touchend' : 'click';
$('#item').on(eventType, function(e) {
//do something
});
Or I also use this as a general switch for touch devices:
var isTouchDevice = (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch);
var eventType = (isTouchDevice) ? 'touchend' : 'click';
This is a total guess but I would assume you would need to create an event handler for each click event you want to ignore, then simply have e.preventDefault() in the function where e is the event object.
This would then run both events simulataniously however the click event would simply do nothing and you would be left with only the code from the ontouchend event.
There could very well be a better way to do this, like I say, only a guess!
Hope it helps :-)
Andrew