views:

40

answers:

3

I use jquery ui to apply a drag and drop effect on a serial of DIVs, for example:

<div class="draggable">...</div>
<div class="draggable">...</div>
<div class="draggable">...</div>
<div class="draggable"> this DIV was dynamically added, not draggable </div>

The problem is dynamically added DIVs won't have this effect applied, how can i apply this effect on new members too?

A: 

Have a look at the jQuery .live(). I believe you could use it here. If not - just attach .draggable() when you create your element.

rochal
A: 

You cannot use the .live() fuction with .draggable() directly, but you can use .live() with the mouseover event and re-attach .draggable() on mouseover like this.

$('.draggable').live('mouseover',function(){
    $(this).draggable();
});
AdmSteck
After a little looking, perhaps it would be better to check it the item is already draggable before making it draggable. $('.draggable').live('mouseover',function(){ if(!$(this).hasClass('ui-draggable')) { $(this).draggable(); } });
AdmSteck
A: 

You might also like to take a loot at the delegate() method introduced with jQuery 1.4 which is slightly better than live()

Here is an article comparing both approaches - http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-the-difference-between-live-and-delegate/

Ivo Sabev