开发者

Use jQuery to attach behavior after a new element has been added to the DOM?

开发者 https://www.devze.com 2023-02-11 00:31 出处:网络
I\'m using jEditable, which turns my div into a nice little form complete wit开发者_如何学Pythonh textarea, Cancel and Submit buttons on-the-fly.

I'm using jEditable, which turns my div into a nice little form complete wit开发者_如何学Pythonh textarea, Cancel and Submit buttons on-the-fly.

After jEditable creates the textarea is created and adds it to the DOM, I'd like to attach the elastic plugin to the element (so the textbox expands and shrinks nicely).

I'm currently using $.live() - which waits for the user to click on the form, THEN attaches the elastic plugin.

It would be cool if I could attach elastic right after the element is created - without messing around with plugin code - is that possible?


Don't use .live(). Instead, after you have initialized the jEditable plugin just chain it with a click event handler

 $(function() {
     $('.edit').editable(
         'http://www.example.com/save.php', 
         {
             type      : 'textarea',
             cancel    : 'Cancel',
             submit    : 'OK',
             tooltip   : 'Click to edit'
         }
     )
     .click(function() {
         $(".edit textarea").elastic();
     });
 });

Try the fiddle
http://jsfiddle.net/Kkg2b/1/

0

精彩评论

暂无评论...
验证码 换一张
取 消