You should remove both inline onclick
and ontouchend
events. Then you may dynamically add event for click
or touchend
based on platform: mobile or desktop. I've made also a fiddle.
Something along these lines:
$(document).ready(function() {
var clickHandler = function(e) {
// Do other stuff on desktop;
alert('desktop');
},
touchendHandler = function(e) {
// Do other stuff on mobile;
alert('mobile');
},
initClickOrTouchEventFor = function(elementSelector) {
var el = $(elementSelector);
// We're on mobile.
if (mobilecheck()) {
console.log('attaching touchend event on mobile');
el.on('touchend', touchendHandler);
}
// We're on desktop.
else {
console.log('attaching click event on desktop');
el.on('click', clickHandler);
}
};
initClickOrTouchEventFor('#clicker');
});