Why/when do I have to tap twice to trigger click on iOS

42,963

Solution 1

I had this same issue. The simplest solution is not to bind the mouseenter event on iOS (or any touch enabled target platform). If that is not bound the hover event won't get triggered and click is triggered on the first tap.

Solution 2

iOS will trigger the hover event if an element is "display: none;" in the normal state and "display: block;" or inline-block on :hover.

Solution 3

I was having this issue using Bootstrap, and I found out that the culprit was the tooltip. Remove the tooltip from the button and you don't need to tap it twice anymore.

Solution 4

I solved this issue by first detecting if it was an iphone, then binding the mouseup event to the function I was trying to call.

if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))){ 
    $('foo').on('mouseup', function(){
        ...
    }
}

I tried other events but mouseup seemed to work best. Other events like touchend were firing even if the user was trying to scroll. Mouseup doesn't seem to get fired if you drag your finger after touching.

Credit David Walsh (and ESPN) for the iPhone detection. http://davidwalsh.name/detect-iphone

Solution 5

It is also worthwhile to mention that ':hover' pseudo-class may prevent 'click' event from firing.

As in mobile browsers click is sometimes used to replace hovering action (e.g. to show dropdown menu), they may trigger artificial 'hover' state on first click and then handle click on the second one.

See https://css-tricks.com/annoying-mobile-double-tap-link-issue/ for detailed explanation and examples of that.

Share:
42,963
Admin
Author by

Admin

Updated on July 19, 2022

Comments

  • Admin
    Admin almost 2 years

    Ok I feel like I'm crazy...

    I'm looking at Mobile Safari on iOs 6.0. I can't seem to establish any rhyme or reason as to when tapping on an element will trigger click. In many cases, it seems I need to tap once to trigger a hover and then again to trigger a click.

    The Mobile Safari spec says : "... The flow of events generated by one-finger and two-finger gestures are conditional depending on whether or not the selected element is clickable or scrollable... A clickable element is a link, form element, image map area, or any other element with mousemove, mousedown, mouseup, or onclick handlers... Because of these differences, you might need to change some of your elements to clickable elements..."

    It goes on to suggest that the developer "...Add a dummy onclick handler, onclick = "void(0)", so that Safari on iOS recognizes the span element as a clickable element."

    However, my testing has shown these statements to be false.

    JsFiddle : http://jsfiddle.net/6Ymcy/1/

    html

    <div id="plain-div" onclick="void(0)">Plain Div</div>
    

    js

    document.getElementById('plain-div').addEventListener('click', function() {
       alert('click'); 
    });
    

    Try tapping the element on an iPad. Nothing Happens

    But I digress. What is important to me is to find out the following question:

    Exactly what are the criteria that determine when clicking on an element will fire a 'click' event on the first tap? As opposed to firing a 'hover' event on the first tap and a 'click' event on the second tap.

    In my testing, anchor elements are the only elements that I can get to fire a click on the first tap, and then, only occasionally and inconsistently.

    Here's where I start to feel crazy. I searched the internet far and wide and found next to nothing about this issue. Is it just me?! Does anybody know where there's been any discussion about the criteria for two-taps and or an approach to dealing with these limitations?

    I'm happy to respond to questions/requests.

    Thanks!

  • JohnA10
    JohnA10 over 9 years
    I solved it using the FastClick JS Pplyfill — github.com/ftlabs/fastclick
  • random_user_name
    random_user_name over 8 years
    This link is now broken.
  • ChrisFox
    ChrisFox almost 8 years
    This seems to have solved the problem for me - thanks.
  • Davi Lima
    Davi Lima about 7 years
  • Ryan
    Ryan about 7 years
    I battled this for hours looking for a million other issues. In my case was using Angular ng-switch and ng-show to show/hide elements (by altering the display CSS value) which caused weird issues with iOS not triggering clicks.
  • Konrad G
    Konrad G almost 6 years
    Wow. This is something really hard to find out. Thanks!
  • basZero
    basZero almost 4 years
    if it does not work on Android, it's not a good solution.