Using jQuery live () to initialize plugins?

Using jQuery, what's the best way to automatically initialize a plugin for all current and future elements of a particular class?

For example, I want all <input type="text" class="datepicker" /> have a jQuery UI <input type="text" class="datepicker" /> plugin, including everything that I could create at runtime.

Essentially, I want to do something like this:

 $('.datepicker').live('create', function() { $(this).datepicker(); }); 

But, of course, there is no creation event that I can use.

+6
javascript jquery jquery-ui
source share
2 answers

You can use the .livequery() plugin for this, reports of his death due to .live() have been greatly exaggerated :)

.live() listens for an event for bubbles, so it fulfills a slightly different purpose. With .livequery() you will achieve what you want:

 $('.datepicker').livequery(function() { $(this).datepicker(); }); 

This will work with current and future .datepicker elements.

+6
source share

From the fact that, as I understand it, you are creating a custom. which is as follows:

 $('.datepicker').bind('foo', { 'bar' : 'bam' }, function(e) { $(this).datepicker(); }); $('.datepicker').trigger('foo'); 

hope this helps

+5
source share

All Articles