0

Does anybody know how to avoid jQuery firing events multiple times after ajax has returned a form.

I have tried with .once() and i am still getting 3 click events from one click.

EDIT: this was my starting point

jQuery('.node_form .form-radio').live('click', function(event) {
  console.log(jQuery(this).attr('id'));                        
});
3
  • can you show the code you written to add jquery Commented Jun 13, 2013 at 5:45
  • @SwastikPareek happens to me too. But for me it happens in element created using ajax by element created using ajax so my code sample would be hard to understand. But I get this using 'method' => 'replace', 'event' => 'blur',
    – Mołot
    Commented Jun 13, 2013 at 6:38
  • What i think is that the ajax has 4 ready state ..and each time states changes the ajax function is called to check whether it is the required state or not... so for the first three times the function is called and hence 3 times the jquery is triggered.. that is what i think Commented Jun 13, 2013 at 7:15

2 Answers 2

1

Can we unbind the click event when click event happens and just add the click event again after we receive some responses from Ajax.

The idea is something like this:

jQuery('.node_form .form-radio').live('click', function(event) {
    jQuery(this).unbind('click');
    //make some ajax calls and if it returns something. We will bind the click event again.
}); 
4
  • For me it happens on blur - and I'm pretty sure I cannot blur my field twice by accident, once I left it, it's left and would need to generate onfocus before next onblur - not the case. So your answer, if helpful in some cases, is far from universal.
    – Mołot
    Commented Jun 13, 2013 at 6:56
  • You're right ! You're very skillful :)
    – Stone Vo
    Commented Jun 13, 2013 at 7:50
  • I'm just a developer who likes problems resolved and I just wanted to note that there is more to this problem than one can see at first glance. I'm not going to downvote your answer or anything (as you see, nobody did it so far), I just want to encourage others to think different and warn future visitors not to assume blindly it'll work.
    – Mołot
    Commented Jun 13, 2013 at 7:51
  • 1
    I have searched the web for some time, and none of the answers did the trick. Yes @Molot, apparently this is really not "a walk in the park".
    – gregab
    Commented Jun 13, 2013 at 8:35
0

Use .one versus .live...like so:

jQuery('.node_form .form-radio').one('click', function(event) {
  console.log(jQuery(this).attr('id'));                        
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.