1

I'm working on a MVC application, containing a search input. The controller sends to the view a list of items that start with the letters present in the input.

My problem is that I want this received items (with the class .suggest) to be draggable, with a clone helper. If I use the following code in the $(document).ready function

$(".suggest").draggable({
    helper: 'clone'
});

the items are not draggable. I suppose this is because the items have been loaded after the page loading?

If I add the class .ui-draggable in the HTML code of the items, it doesn't work neither. Is there a solution to drag elements loaded by a controller?

tereško
  • 58,060
  • 25
  • 98
  • 150
Elodie
  • 108
  • 9
  • Have you tried jQuery's [live](http://api.jquery.com/live/)? – Waleed Khan Aug 03 '12 at 14:07
  • Did you include the correct libraries? – Arcturus Aug 03 '12 at 14:08
  • Yes, the correct libraries are included : if I try this code on items that are loaded at the beginning, it works.. – Elodie Aug 03 '12 at 14:11
  • `$(document).ready` will fire exactly one time. You'll need to use `draggable` on your elements *after* they have been loaded. You'll have to include a part from the controller code, namely the creation and append of the `.suggest` elements in order to get satisfying answers. – Zeta Aug 03 '12 at 14:20
  • Thank you for your help. I've included a function $(document).ready in the message sent to the view, and it works. But is it a good way to make the message longer between the controller and the view ? – Elodie Aug 03 '12 at 14:30

1 Answers1

1

Try this topic:

jQuery Drag And Drop Using Live Events

Community
  • 1
  • 1
Jorge Loureiro
  • 458
  • 3
  • 11
  • I found an other solution : when I receive the information from the controller, I call a javascript function that sets the draggable attribute, and the helper function. – Elodie Aug 03 '12 at 14:42