thatOneGuy thatOneGuy - 22 days ago 7
Javascript Question

Simulate click event on mobile device

I have a click event on window load like so :

function myFunction(){
document.getElementById('myEl').click(); // works on desktop, not on mobile
}

window.onload = myFunction;


As mentioned, this works on desktop but not on mobile devices. I gathered it was down to mobile handling click events differently. Is that correct ?

I need my element to be 'clicked' on load on all devices, desktop and touch pads (mobiles etc).

Answer
function myFunction() {
    $("#myEl").trigger('click');
    $("#myEl").trigger('touchstart');
}
window.onload = myFunction;

For touch event, visit below link.

https://developer.mozilla.org/en-US/docs/Web/Events/touchstart