Globulopolis
Globulopolis

Reputation: 85

Bind plugin action for on-the-fly elements

I've an input field that created on the fly. And I need to bind a plugin interaction from jQueryUI widget.

My code

$("select[name='tab_content_length']").live('click', function(){
    $(this).selectmenu({ style: "dropdown"});
});
$("select[name='tab_content_length']").trigger('click');

This is crutches and second part won't work. Any ideas how to bind plugin interaction to on-the-fly element?

PS! And I can't and won't bind to 'click', but I don't known any over solutions :(

Upvotes: 0

Views: 198

Answers (1)

Nick Craver
Nick Craver

Reputation: 630389

This is a purpose the .livequery() plugin still serves:

$("select[name='tab_content_length']").livequery(function(){
    $(this).selectmenu({ style: "dropdown"});
});

Another way would possibly be to use .live() on a prior event and check if it's been bound...it depends how the plugin is written though, for example mousedown happens before click (when a user clicks), like this:

$("select[name='tab_content_length']").live('mousedown', function(){
  if(!$.data(this, 'menubound')) //prevent re-binding the plugin
    $(this).selectmenu({ style: "dropdown"}).data('menubound', true);
});

Upvotes: 2

Related Questions