Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate click event on mobile device

Tags:

javascript

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).

like image 212
thatOneGuy Avatar asked Jul 26 '26 16:07

thatOneGuy


1 Answers

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

like image 123
AsgarAli Avatar answered Jul 28 '26 15:07

AsgarAli