1

What i would like to do is trigger a button's click event from within a view that gets returned from an ajax call. This process works fine in a pc browser such as chrome but not so in a mobile browser. (i am using both jquery and jquery mobile).

(there is a lot more code involved in this but i have removed it for clarity)

I have a button in my html page like so.

 <input type="button" id="bt1" value=""  />

It has an onclick event listener on it defined somewhere else.

I have an ajax call that calls a php script like so:

 $.ajax({
        url: 'blah.php',
        type: 'get',
        data: {
            id : $('#field').val()
        },
        dataType: 'html',
        success: function (data) {
           $('#somediv').html(data);
        }
    });

It returns "data" which is a segment of html with inline javascript. It containts the following code:

<div>
<script type="text/javascript">
    $(document).ready(function () {
      $("#bt1").trigger("click");
     });
</script>
</div>

What ive noticed is that the trigger event will not be fired when used in a mobile. but works fine in pc.

Is there a different in the DOM when dealing with mobile that it prevents a trigger("click") from working if the object is outside the view?

Notaras
  • 611
  • 1
  • 14
  • 44

3 Answers3

-1

Try adding the touchstart event along side the click event.

$("#bt1").trigger("touchstart click");
zgood
  • 12,181
  • 2
  • 25
  • 26
  • i tried this but it didnt work. I do in fact use `trigger(click)` in other places of the system and it works fine. The only difference in this case is that the button element im trying to trigger is outside the view that is being returned. Thanks for your help though – Notaras Mar 20 '15 at 04:53
-1

you may need to implement touch events on mobile. Take a look at this SO answer:

Understanding touch events

If you cannot change what comes back from the server then you can listen for a click event and trigger a touch event.

Good luck, Mike

Community
  • 1
  • 1
Mike Wrather
  • 146
  • 7
-1

It cannot find the button since the javascript code and the button element is being created programatically, try:

$(document).find('#bt1').trigger('click');

or

$(document).find('#bt1').each(function(e) {
    e.trigger('click');
}

or, better:

success: function (data) {
    $('#somediv').html(data).promise().done(){
       $('#bt1').trigger('click');
    });
}
clintgh
  • 2,039
  • 3
  • 28
  • 44