41

we have a web application which is using Jquery blockUI to open a pop up and do some action. All of this works fine on Safari, and IE 8. problem is with Ipad. none of the actions in pop up are responding. it just stays on that page. even close doesnot work. do we need to add anything else? here is the code that opens a page and click event for close.

<script>
$(document).ready(function() {
  $.ajaxSetup( {
           cache:false
   });

        $("#sendInviteDiv").load("invite.htm?action=view&pid="+pid);
            $.blockUI({ message: $('#sendInviteDiv'),
                centerY: 0,
                    css: {
                top:  ($(window).height() - 550) /2 + 'px',
                        left: ($(window).width() - 870) /2 + 'px',
                        width: '870px'
                }
            });
            //var ua = navigator.userAgent;
            //var event = (ua.match(/iPad/i)) ? "touchstart" : "click";
            //alert(ua);

            $('#closeInvite').click($.unblockUI);

    $('#inviteBtn').click(function() {
//script to load 
       //setPositionDetails('${formName}','inviteBtn');

       });
}


});


</script>

appreciate pointers.

javascript is turned on and popups are allowed in Ipad Safari settings.

jrummell
  • 42,637
  • 17
  • 112
  • 171
Atchuta Vani
  • 431
  • 1
  • 4
  • 6
  • 2
    Turn on the debug console in the Safari prefs (Settings - Safari - Developer). Any error messages? – steveax Oct 25 '11 at 17:00

11 Answers11

86

I usually use

.bind("click touchstart", function(){

});

instead of:

.click(function(){

});

That way you are binding the the correct event. It's also quicker, the touch responds much faster than click for some reason.

Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
  • 14
    In my case I had to use bind("click tap", ...) – schmunk Nov 23 '11 at 15:13
  • this is what the developers of webkit should note somewhere at the very beginning, not us trying to find out the problem. otherwise, good finding, Rich! – Alex K Aug 12 '13 at 07:11
  • The event has to be bound directly to the element for iOS to register events on that element. That means that delegated event handlers won't work (`.on("click touch", "selector", function() { … }` won't work). – thirdender Aug 18 '13 at 18:01
  • 2
    bind to 'touchstart' seems to do the trick, touch or tap didn't for me? – tonycoupland Aug 29 '13 at 15:08
  • 4
    I also find the same that `$('v').on('touchstart'...)` is working where click, tap and touch don't. Have definitely had on('click') work when elements were individually registered. `touchstart` isn't ideal as it happens when you are expecting to scroll but happen to touch an element. – PeterT Nov 19 '13 at 14:46
  • @thirdender Do you happen to have any reading that describes this behavior? I'm curious to learn more about it. – aebabis Feb 28 '14 at 22:32
  • @acbabis – See [QuirksBlog: Click event delegation on the iPhone](http://www.quirksmode.org/blog/archives/2010/09/click_event_del.html) and the follow-up [QuirksBlog: Click event delegation on the iPhone – redux](http://www.quirksmode.org/blog/archives/2010/10/click_event_del_1.html). This is very old information by now, things may have changed. Events do bubble, but events are not dispatched for non-link, non-form input elements, _unless_ a click handler was added for that specific element. Example: `LABEL` elements do not dispatch a click event unless the listener is bound to that `LABEL`. – thirdender Mar 01 '14 at 00:04
  • I think using the [`touchend`](https://developer.mozilla.org/en-US/docs/Web/Events/touchend) event "feels" better than `touchstart`. But nevertheless, this fixed the problem for me as well. – Luiz Aoqui Oct 14 '15 at 20:18
  • .bind("click", function(){ }... was the only one that worked for me after trying many other. – JohnA10 Jan 22 '16 at 06:52
  • 1
    Using touchstart/touchend is not a good solution (for user experience). It will trigger the event if a user is trying to scroll down the page if they happen to touch your button. See http://stackoverflow.com/a/17490775/1608100 for a useful solution. Seems click events will be registered on ios for items with cursor:pointer; an tag by default will trigger that click event. Whereas other divs or elements that need click events on ios but don't have cursor:pointer won't fire. It's kind of a dumb problem for ios. – nwilson5 Apr 13 '16 at 17:53
  • Worth noting as this is an old answer this still did the trick in October 2018. Nice, thanks. – dartacus Oct 19 '18 at 12:32
  • 1
    `touchstart` worked but doesn't allow to scroll and `cursor:pointer` does not work. – taher Jan 05 '19 at 14:03
37

I know this was asked a long time ago but I found an answer while searching for this exact question.

There are two solutions.

You can either set an empty onlick attribute on the html element:

<div class="clickElement" onclick=""></div>

Or you can add it in css by setting the pointer cursor:

.clickElement { cursor:pointer }

The problem is that on ipad, the first click on a non-anchor element registers as a hover. This is not really a bug, because it helps with sites that have hover-menus that haven't been tablet/mobile optimised. Setting the cursor or adding an empty onclick attribute tells the browser that the element is indeed a clickable area.

(via http://www.mitch-solutions.com/blog/17-ipad-jquery-live-click-events-not-working)

lachie_h
  • 559
  • 1
  • 6
  • 15
  • 1
    cursor:pointer worked wonderfully - only solution I found to be working with live() for dynamically created tables through ajax. – Johncl Jun 24 '13 at 11:46
  • So another solution to this problem is changing your
    to an .
    – Esger Nov 27 '13 at 09:39
  • @Esger True, but that wouldn't have worked for my situation which I didn't detail. I had a sortable table and it's not easy to wrap a table row element in an anchor tag. Thanks for expanding though! – lachie_h Dec 11 '13 at 21:51
  • 3
    I had to add an empty onclick even after I changed my div tag to an a tag. Just the a tag did not seem to trigger the click. – akotian May 06 '14 at 23:22
  • Didn't see this answer while looking for the same thing, so solved my problem adding `onclick="return false;"` to my div element, which also works fine. – Alex Sep 25 '14 at 08:14
25

UPDATE: I switched .bind to .on because it's now the preferred way says jQuery.

As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document. jquery.com

EXAMPLE:

$('.button').on("click touchstart", function() {

});

Below is the ultimate version of the click and touchstart event because it still fires even with new DOM objects that are added after the DOM has been loaded. Incorporating this ultimate click event solution for any scenario.

$(document).on("click touchstart", ".button", function () {

});
Juan
  • 4,910
  • 3
  • 37
  • 46
BBi7
  • 1,588
  • 1
  • 11
  • 8
7

Use bind function instead. Make it more friendly.

Example:

var clickHandler = "click";

if('ontouchstart' in document.documentElement){
    clickHandler = "touchstart";
}

$(".button").bind(clickHandler,function(){
    alert('Visible on touch and non-touch enabled devices');
});
tonycoupland
  • 4,127
  • 1
  • 28
  • 27
Samson
  • 71
  • 1
  • 2
  • And this skips the delay of a click event on touch devices. – Dominique Alexandre Apr 28 '15 at 17:58
  • 1
    Using variable bindings was the way to go for my particular situation. If any user is having issues with the event firing twice in mobile because of duplicate binding, this is the way to go. – KoldBane Jun 27 '16 at 15:04
2

Thanks to the previous commenters I found all the following worked for me:

Either adding an onclick stub to the element

onclick="void(0);" 

or user a cursor pointer style

style="cursor:pointer;"

or as in my existing code my jquery code needed tap added

$(document).on('click tap','.ciAddLike',function(event)
{
    alert('like added!'); // stopped working in ios safari until tap added
});

I am adding a cross-reference back to the Apple Docs for those interested. See Apple Docs:Making Events Clickable

(I'm not sure exactly when my hybrid app stopped processing clicks but I seem to remember they worked iOS 7 and earlier.)

Anthony De Souza
  • 544
  • 5
  • 10
1

Probably rather than defining both the events click and touch you could define a an handler which will look if the device will work with click or touch.

var handleClick= 'ontouchstart' in document.documentElement ? 'touchstart': 'click';
$(document).on(handleClick,'.button',function(){
alert('Click is now working with touch and click both');
});
Abhishek
  • 411
  • 8
  • 19
  • 2
    That is not a good solution. There are devices outhere that can have both mouse and touch. – jhruby Mar 12 '14 at 15:08
0

We have a similar problem: the click event on a button doesn't work, as long as the user has not scrolled the page. The bug appears only on iOS.

We solved it by scrolling the page a little bit:

$('#modal-window').animate({scrollTop:$("#next-page-button-anchor").offset().top}, 500);

(It doesn't answer the ultimate cause, though. Maybe some kind of bug in Safari mobile ?)

Eino Gourdin
  • 4,169
  • 3
  • 39
  • 67
0

I had a span that would create a popup. If I used "click touchstart" it would trigger parts of the popup during the touchend. I fixed this by making the span "click touchend".

Luke Wenke
  • 1,149
  • 2
  • 23
  • 43
0

I found that when I made my binding more specific, it began to work on iOS. I had:

$(document).on('click tap', 'span.clickable', function(e){ ... });

When I changed it to:

$("div.content").on('click tap', 'span.clickable', function(e){ ... });

iOS began responding.

djbp
  • 714
  • 8
  • 24
0

None of the upvoted solutions worked for me. Here's what eventually worked:

I moved the code which was in $(document).ready out, if it wasn't required in document ready. If it's mandatory to have it in document ready, then you move that critical code to jQuery(window).load().

anothernode
  • 5,100
  • 13
  • 43
  • 62
0

actually , this has turned out to be couple of javascript changes in the code. calling of javascript method with ; at the end. placing script tags in body instead of head. and interestingly even change the text displayed (please "click") to something that is not an event. so Please rate etc.

turned debugger on safari, it didnot give much information or even errors at times.

Atchuta Vani
  • 431
  • 1
  • 4
  • 6