19

I have a set of Div's which act as buttons. These buttons have a simple jquery click() function which works in all browsers except iOS.

For example:

<div class="button">click me</div>

and

$('.button').click(function(){

   alert('hi');

});

I'm not sure why this doesn't work on iOS - clearly there is no 'clicking' in iOS.

Unfortunately I don't have an iphone device to test this myself, but I'm getting a lot of complaints from my clients who want to click things and nothing is happening.

What is the key to getting this working?

mrtsherman
  • 39,342
  • 23
  • 87
  • 111
willdanceforfun
  • 11,044
  • 31
  • 82
  • 122

5 Answers5

37

Click ": means When a mousedown and mouseup event occur on the same element OR an element is activated by the keyboard.

from Jquery Bug, there is work around , Just add "cursor: pointer" to the element's CSS and the click event will work as expected. and you can even see this Jquery click on Ios for help

Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
7

Actually, the accepted answer did not work for me. I tried using "cursor:pointer", onclick="", and even convert the element to an anchor tag.

What i did to make it work is to bind the touchstart event insted of the click event. To make it work on all platforms i had to to an ugly ua spoofing like this:

var ua = navigator.userAgent,
event = (ua.match(/iPad/i) || ua.match(/iPhone/)) ? "touchstart" : "click";

jQuery("body").on(event, '.clickable_element', function(e) {
    // do something here
});
Marek Maurizio
  • 1,073
  • 2
  • 11
  • 22
  • 2
    Totally works for me! Rewrote it without the `jQuery("body")`though, and used a little less confusing var name instead of 'event', but I guess the latter was just for clarifying purposes. – cptstarling Apr 23 '16 at 20:59
2

Based on Marek's answer this is my take on it (it's a bit cleaned up):

var ua = navigator.userAgent, 
pickclick = (ua.match(/iPad/i) || ua.match(/iPhone/)) ? "touchstart" : "click";

$('.clickable_element').on(pickclick, function(e) {
     // do something here
});

There's still a lot of work ahead for the industry when it comes to standards…

EDIT:

Didn't work out on Android phones. Took a more rigid approach:

if (document.documentElement.clientWidth > 1025) { pickclick = 'click' }
if (document.documentElement.clientWidth < 1025) { pickclick = 'touchstart' }

$('.clickable_element').on(pickclick, function(e) {
     // do something here
});
Community
  • 1
  • 1
cptstarling
  • 769
  • 6
  • 11
2

Also based on Marek's answer, which adapts the event to the device to fire a function, here is a code that force to fire a click on iOS devices, where there is first a touchstart event :

var UA = navigator.userAgent,
iOS = !!(UA.match(/iPad|iPhone/i));

if (iOS) {
   $(document).on('touchstart', function (e) {
       e.target.click();
   });
}
Community
  • 1
  • 1
0

You can use this:

$('button').bind( "touchstart", function(){
    alert('hi');
});

Another Solution is to set the element's cursor to pointer and it work with jQuery live and click event. Set it in CSS.

SteppingHat
  • 1,199
  • 4
  • 19
  • 50
Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25