As far as I can tell, I think this has to do with passive event listeners and event propagation on the touchstart
event. When I tried to add prevenDefault
to your even handler like so:
$(function() {
$(document).on('click touchstart', '.js-next', function(e){
e.preventDefault();
$('.js-tab').click();
});
});
I get this warning in chrome:
Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080
Following that link you'll see this in the docs:
AddEventListenerOptions defaults passive to false. With this change touchstart and touchmove listeners added to the document will default to passive:true (so that calls to preventDefault will be ignored).
Chasing that down I came across this answer on SO: https://stackoverflow.com/a/39187679/1819684 Which says that jQuery has an ongoing issue with passing options to addEventListener
. Meaning that at the moment you can't do it with jQuery and set passive: false
. So I reworked your example code to this: https://jsfiddle.net/w9h4an81/
$(function() {
document.addEventListener('touchstart', function(e) {
e.preventDefault();
$('.js-tab').click();
}, {passive: false});
$(document).on('click', '.js-next', function(e){
$('.js-tab').click();
});
});
To do it only with jQuery you have to do something like this: https://jsfiddle.net/6377wdhc/ You can't listen on the document
.
$(function() {
$('.js-next').on('click touchstart', function(e){
e.preventDefault();
$('.js-tab').click();
});
});