I have now tried to find a solution for this problem for a couple of hours now and apparently I can't solve this problem myself.
So the idea itself is quite simple. I want to trigger a click for #game-undo
and it works just fine for desktop. But the code below will result in ghost clicks when it comes to mobile and tablets.
$(document).on('click tap touchstart','.game-undo-btn',function(event){
$('#game-undo').trigger('click');
});
So the first solution I tried was the following:
$(document).on('click tap touchstart','.game-undo-btn',function(event){
if(event.type == "click") {
$('#game-undo').trigger('click');
console.log('click triggered');
}
else if(event.type == "tap") {
$('#game-undo').trigger('click');
console.log('tap triggered');
}
else if(event.type == "touchstart") {
$('#game-undo').trigger('click');
console.log('touchstart triggered');
}
});
My logic from PHP tells me that if the event.type
is click
then the rest of the else statements wont be executed. I guess I'm wrong because the console tells me that both click and touchstart is triggered.
I then tried to play around with event.preventDefault();
but I quickly found out that this was not possible because when touchstart
is added - the event listener will be treated as passive.
The message in the console for using event.preventDefault();
is:
[Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive.
Okay - so the next step for me was to play around with event.stopPropagation();
- but this is ignored and the else statements will be executed anyway.
There is no duplicate code.
I hope you guys can help me out. Thank you.