Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile Safari sometimes does not trigger the click event

Tags:

This is a dynamic JavaScript application I am working on. I have many <a> anchor elements that have a class. When that class is clicked, something should happen. This works fine in Firefox, Chrome, IE, but in some cases the click event is not triggered on mobile Safari (iPad and iPhone).

These elements all have exactly the same CSS, it's just their position that differs (they are in different containers).

I tried various solutions that I found here but with no luck. For instance:

  • setting the cursor to pointer
  • the code in this answer

Do you have any other idea that might help me find a solution to this? Why does the click event triggers only in some cases?

like image 719
watt Avatar asked Jun 06 '14 08:06

watt


2 Answers

The click event resulting from a tap on iOS will bubble as expected under just certain conditions -- one of which you mentioned, the cursor style. Here is another:

The target element, or any of its ancestors up to but not including the <body>, has an explicit event handler set for any of the mouse events. This event handler may be an empty function.

http://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html

This is much better fix vector, and can be done without a browser check. You do have to add an extra div due to the "not including the <body>" part:

<body>   <div onclick="void(0);">     <!-- ... all your normal body content ... -->   </div> </body> 

Now every click event originating inside that div will bubble as expected in iOS (meaning you can catch them with $(document).on('click', '.class', etc...)).

like image 67
LinusR Avatar answered Sep 21 '22 18:09

LinusR


Have you tried this?? This is because ios doesn't fire the click event sometimes, only recognizes the touch event

$(document).on('touchstart click', [Selector], [ Event ] 
like image 25
Gopinath Koothaperumal Avatar answered Sep 17 '22 18:09

Gopinath Koothaperumal