0

At the moment, I have a piece of code looking like this:

$("#somediv").append(data);
somethingToDoAfterDataAppended();

It seems that the data is appended asynchronously, therefor the next function is not necessarily invoked after data is actually appended.

I was thinking about a way to bind this function with 'data appended' event - is it possible?

Any other solution would be equally useful.

  • Check this out: http://stackoverflow.com/questions/6068955/jquery-function-after-append – woot Aug 11 '12 at 12:22
  • Or also [this](http://help.dottoro.com/ljrmcldi.php) or [this](http://stackoverflow.com/questions/3219758/detect-changes-in-the-dom)? – Stano Aug 11 '12 at 12:45
  • `.append` is always synchronous. Whatever the problem is, it's somewhere else. Try to reproduce the issue in a live scenario. – Fabrício Matté Aug 11 '12 at 13:45
  • Sorry, you were right. It is synchronous, it was my mistake of invocation placing. Again. sorry, thanks for your help. – user1364136 Aug 11 '12 at 14:57
  • No problem, I'd remove my downvote but I can't if you don't edit the question (or post an answer with the solution you've found). – Fabrício Matté Aug 11 '12 at 15:02

1 Answers1

-1

It simple. You can change the append method and insert there a trigger(s) for you special event.

$.fn.append = (function(old){
   return function(){

       $(this).trigger('beforeAppend'); // if needed
       var result = old.apply(this,arguments);
       $(this).trigger('afterAppend');
       return result;

       }

    })($.fn.append);

Somewhere in your initialization logic:

$("#somediv").bind('afterAppend',somethingToDoAfterDataAppended);

And :)

("#somediv").append(data); 
abuduba
  • 4,986
  • 7
  • 26
  • 43