Jack_of_All_Trades
Jack_of_All_Trades

Reputation: 11468

creating draggable on the fly

How can a draggable in jquery UI be created on the fly with response to an event? Since event can be multiple, how can each freshly created draggable be assigned a different ID for the forthcoming use?

Upvotes: 1

Views: 481

Answers (3)

Matthew
Matthew

Reputation: 8416

You make an element draggable with $("#your-id").draggable();

Do you want to create a new element during an event like this?

function yourEventHandler()
{
 $("BODY").append(
  $('<DIV id="#my-div" style="width:20px;height:20px;background-color:red"></div>')
 );
 $("#my-div").draggable();
}

Upvotes: 1

MyStream
MyStream

Reputation: 2543

In jQuery you can use the .delegate() method to listen for the source of the event (e.g. a click?) and then you can use the "target" argument to bind jQuery UI .draggable() to the target - probably like this:

$(event.target).draggable(objDragSettings);

where objDragSettings defines any presets.

Upvotes: 1

Shaheer
Shaheer

Reputation: 2147

try this:

var currItems = 0;

$('<div>').attr('id', currItems++).appendTo('body').draggable();

Upvotes: 2

Related Questions