Access AJAX Returned Elements with JQuery’s .delegate()

I love the way jQuery handles AJAX. With jQuery you can do so much more with so much less code. So once an AJAX call returns some HTML containing elements that you wish to manipulate further with jQuery how do you access it? You may have noticed that AJAX returned elements are not visible to jQuery. Since AJAX is asynchronous the document ready has already been established so for instances like these we need to think ahead. Fortunately there is a very cool jQuery core function named .deligate().

.deligate() is used to attach a handler to one or more events for all the elements that match the selectore. This happens for both the present and the future and is based on a set of root elements.

By using delegate() we can prepare jQuery to look for elements that have net been created yet. So, when your AJAX calls return their resulting elements now you can work with them using jQuery.

Leave a Reply

Your email address will not be published. Required fields are marked *