Search code examples
jquerybindinglive

How to auto bind plugin initializations for content loaded through ajax


I'm using expandable plugin by Brandon Aaron to auto grow my textareas in the form and it is working ok. the problem is that I'm loading a lot of pages through ajax so I need to rebind the plugin method to the textareas loaded through ajax.

is there a way to do this kind of method calls like

$("textarea").expandable();

through live() or delegate() in jQuery. it'll make my code much cleaner.


Solution

  • The .liveQuery plugin still has a use here, it's not entirely replaced by jQuery core's .live(), here's how your example would work:

    $("textarea").liveQuery(function() {
      $(this).expandable();
    });
    

    .livequery() actually watches for new elements, unlike .live() which listens for events to bubble up the DOM, they operate in very different ways.