8

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

$('.myviewer').click();

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

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

All work on a computer but not an iPad. What am I doing wrong?

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

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

And to round this out 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);
};      

});

user278859
  • 10,379
  • 12
  • 51
  • 74
  • possible duplicate of [How to recogized touch event using Jquery for ipad safari browser? Is it possible?](http://stackoverflow.com/questions/4755505/how-to-recogized-touch-event-using-jquery-for-ipad-safari-browser-is-it-possibl) – Esailija Jun 03 '12 at 20:33
  • I doubt that code even works on a computer as you are not passing any function to `.bind`... – Esailija Jun 03 '12 at 20:48
  • Let me clarify. I am not calling all of those all together. I was just showing what I have tried. Each is a way that I have tried to trigger the event... a single line of code. For example $('.myviewer').click(); works on a computer all by itself. You are right though I don't think the bind calls should be in there. I will remove them. Also, I don't think this is a duplicate the other is "How to recognize a touch event" my question is how to trigger the event. – user278859 Jun 03 '12 at 21:30
  • Please add the code how you are adding touchstart and click events in the first place, like I said "html events" will not work here, even if they appear to work for click (which is a very special case). – Esailija Jun 03 '12 at 21:32
  • I have added both the html and the jquery code I am using. – user278859 Jun 03 '12 at 21:57

1 Answers1

11

For .trigger to do anything, you must bind the event first, which you haven't done. onclick="" doesn't count.

To bind the event first use:

$(document).ready(function() {
    $('.myviewer').on( "touchstart", function(){
        $(this).remove();
    });

    var isMobile = { //...your original code continues here

Then you can later trigger it:

$('.myviewer').trigger('touchstart');
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • Thanks. The window.open in the div does not work. If that is because it is in the html, then I am a bit mystified as to why .click() on computers work. In any event I have resolved the issue that brought me to this question, why window.open doesn't work from jquery on the iPad, so I am moving on. Thanks for you help. – user278859 Jun 03 '12 at 22:46
  • @user278859 I explained it a while ago, `click()` is very special case and it was an "accident" it worked with "html event" and `.trigger` – Esailija Jun 03 '12 at 22:53