Skip to content Skip to sidebar Skip to footer

$(document).ready(function(){} Jquery Is Getting Called Twice?

Im new to jquery..I have written a jquery function to process some form inputs. I see an strange issue that my $(document).ready(function(){} is getting called twice. My form is;

Solution 1:

You're nesting event handler assignment, which is usually a bug. You've got:

$('#publish_api').click(function(e){

which establishes a handler for the "click" event from an element. Inside that event handler is code that sets up another event handler:

    $("body").on("api_saved", function(e){
        alert("calling lifecycle jag");
        // ...

The reason that's likely to be a bug is that every call to .on() inside the "click" handler will attach a separate copy of that event handler. After clicking twice, there will be two identical handlers for the "api_saved" event. After clicking 5 times, there'll be 5 handlers, and so on. That happens because a call to .on() does not remove event handlers that are already registered.

Probably the right thing to do is move that event handler assignment (the one for "api_saved") out of the "click" handler.

Post a Comment for "$(document).ready(function(){} Jquery Is Getting Called Twice?"