What is the correct way to trigger touch event on iPad using jquery

I tried the following (.myviewer is a div) ...

$('.myviewer').click(); and $('.myviewer').trigger('touchstart'); and $('.myviewer').trigger('click'); 

Everyone works on a computer, but not on an iPad. What am I doing wrong?

Here is what the html page body looks like ...

 <body> <div class="myviewer" onclick="window.open('myPDFFile.pdf');">Programmatically clicked</div> </body> 

And to get around this, here is my jquery code ...

 $(document).ready(function() { var isMobile = { Android : function() { return navigator.userAgent.match(/Android/i) ? true : false; }, BlackBerry : function() { return navigator.userAgent.match(/BlackBerry/i) ? true : false; }, iOS : function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false; }, Windows : function() { return navigator.userAgent.match(/IEMobile/i) ? true : false; }, any : function() { return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows()); } }; if(isMobile.any()) { $('.myviewer').clck(); //this does works on computers but not on iPad }else { var markup = "<object data='myPDFFile.pdf#toolbar=1&amp;navpanes=1&amp;scrollbar=0&amp;page=1&amp;view=FitH' type='application/pdf' width='100%' height='100%'> </object>"; $('.myviewer').append(markup); }; 

});

+8
jquery triggers ipad
source share
1 answer

In order for .trigger to do anything, you must first bind an event that you have not yet done. onclick="" does not count.

To associate an event with first use:

 $(document).ready(function() { $('.myviewer').on( "touchstart", function(){ $(this).remove(); }); var isMobile = { //...your original code continues here 

Then you can activate it later:

 $('.myviewer').trigger('touchstart'); 
+9
source share

All Articles