0

I have a problem with my Ajax call. In success function, I want to trigger a click on a element but it doesn't fire.

Here is the code :

$.ajax({
    url : '/test/'+clubID,
    beforeSend:function(){
        $.mobile.showPageLoadingMsg();
    },
    complete:function(){
        $.mobile.hidePageLoadingMsg();
    },
    success:function(data, textStatus, jqXHR){
        if(!jQuery.isEmptyObject(data)) {
            $('#camera-input').click();
            console.log($('#camera-input'));
            console.log('click');
        } else {
            toast('Il est possible de prendre des photos uniquement lors d\'une soirée');
            console.log('toast');
        }
    }
});

The console print click and the element. Any ideas about the problem ?

Thanks for your help

bidou88
  • 694
  • 2
  • 8
  • 31
  • 2
    What do you mean by "trigger a click"? This triggers click handlers, but it doesn't prompt the native click behaviour. – lonesomeday Jun 24 '13 at 15:51
  • Do you have code that bound to the click event you are executing? Make sure your bind is actually setup. – CodeMonkeyForHire Jun 24 '13 at 15:53
  • Does $('#camera-input') return the expected element? When calling it in the debugger you can also see there if the click handler that you want to call is actually bound. – Sandro Jun 24 '13 at 15:57
  • if I put $('#camera-input').click(); before the ajax call, it opens the windows to choose a file. I want to reproduce that, but in the success function – bidou88 Jun 24 '13 at 15:59
  • Yes, it returns true and there is no error in the console. Maybe there is a problem with jQuery Mobile and Ajax request. – bidou88 Jun 24 '13 at 16:38

2 Answers2

0

Give this a try... this is my research, not my experience. No guarantees.

You may need to bind the event handler to a common ancestor of the elements on which it should be triggered. For example, if your #element gets appended inside a div with an id of parent:

$("#parent").on("click", ".commenticon", function() {
    //Do stuff
});

This would be for an HTML structure like so:

<div id="parent">
    <div class="element">

    </div>
    <div class="element">

    </div>
</div>

DOM events bubble up the tree from the point at which they originate. The on() (formerly delegate - jQuery pre 1.7) method captures the event at an ancestor element and checks whether or not it originated at an element matching the selector.

Source

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
0

try this,

$('#camera-input').trigger('click');

Kautil
  • 1,321
  • 9
  • 13