views:

33

answers:

1

I'm using the following jQuery plugin and it executes when there's a click action on the specified element :

http://www.andresvidal.com/labs/relcopy.html

I also created a click function for the same element, but I expected it to execute after the plugin. However that isn't the case, the click function always executes first, thus causing problems.

Please check my code below for example, I'm new to jQuery so any suggestions / help would really be appreciated!

$.getScript('../js/relCopy.js', function() {

            $('#AddTable').relCopy({
                limit: 10,
                excludeSelector: '.newListSelected'
            });

$('#AddTable').click(function() {
    do something here after relcopy has finished
});
A: 

Event handlers are executed in the order they were bound, and since the callback from $.getScript() executes after that script is loaded, it's binding it's click handler after yours.

So get the order you want you need to bind in the order you want, that means binding your click handler in the callback as well, like this:

$.getScript('../js/relCopy.js', function() {
  $('#AddTable').relCopy({
    limit: 10,
    excludeSelector: '.newListSelected'
  }).click(function() {
    //do something here after relcopy has finished
  });
});
Nick Craver
Thanks, worked perfectly! Do you mean the callback from $.getScript() only executes after the relCopy.js has been loaded? If thats the case, shouldn't it finish executing relCopy.js before moving on to my click handler?
@user435216 - Yes for the first question, no for the second... `$.getScript()` is an asynchronous operation, it finishes when the response comes back and it executes the `success` callback then :)
Nick Craver