Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery delegates with plugins

jQuery delegates are great, especially when using with table row click events.

I was wondering if it's possible to use delegates with plug-ins as well?

For example if I attach elastic plug-in to every text area, I would do:

$("textarea").elastic();

But how would I attach this plug-in using delegate?

like image 625
Daniil Harik Avatar asked May 09 '26 02:05

Daniil Harik


2 Answers

You can cheat. Well, you can in this instance.

You need a delegate-able event that will always occur before the plugin needs to be applied and some means of knowing whether or not it already has been. You can always add that yourself though.

Assuming you are on about this plugin:

http://www.unwrongest.com/projects/elastic/

then, since you always have to focus a textarea before typing/pasting into it, you can do this:

$('textarea').live( 'focus', function(){
   if( !$(this).data('iselastic') )
      $(this).data('iselastic', true).elastic();
})

I'm assuming you're running jQuery 1.4 at least.

like image 188
xiani Avatar answered May 12 '26 06:05

xiani


Its really up to the plugin author to write the plugin to utilize event delegation. Live and delegate won't work in applying a plugin.

like image 21
PetersenDidIt Avatar answered May 12 '26 05:05

PetersenDidIt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!