Summer
Summer

Reputation: 2498

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

I'm using jEditable, which turns my div into a nice little form complete with 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?

Upvotes: 0

Views: 473

Answers (1)

Nimrod
Nimrod

Reputation: 1346

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/

Upvotes: 2

Related Questions