jQuery: how to handle events after an Ajax request

In my previous post I said that I had some problems with events after an Ajax request. Problem solved: you have to use the success() method of the $.ajax() wrapper. For example:

$.ajax({
    //... more methods/properties here
    success: function(html) {
        $(html).appendTo('body');
        $('p').click(function() {
            alert('OK');
        });
    }
});

The fact is that the success() method encompasses also the DOM events occurring after the response is complete and the fetched content is retrieved.

Leave a Reply

Note: Only a member of this blog may post a comment.