31

I've got a bunch of elements with jQuery. Some are draggable, some are droppable and some are both. How can I detect if an element is draggable or droppable?

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
  • Why can't you use the original selector used when instantiating `droppable` or `draggable`? – zzzzBov Nov 21 '11 at 19:02
  • @zzzzBov, my conrol is loaded using AJAX, so I don't know what happened on the document. – Kees C. Bakker Nov 21 '11 at 20:48
  • please show some code. I have no idea what you're talking about. If you're sending some HTML via AJAX, then why does it matter whether the element is draggable or droppable? – zzzzBov Nov 21 '11 at 21:17
  • @zzzzBov an example is very difficult to create. Basically, I have a page... my js is loaded (with ajax), it needs to find all draggables and droppables at that moment and post the number of elements back to the server for logging. – Kees C. Bakker Nov 21 '11 at 21:21
  • and *why* do you need to log the number of draggables and droppables? – zzzzBov Nov 21 '11 at 21:26
  • Because every draggable and droppable represents a certain object. The page needs to start logging the state at certain moments (or when objects are in a certain state) to determine the performance of the user. – Kees C. Bakker Nov 21 '11 at 21:32

5 Answers5

27

You could also use jQuery data() like this..

if ($(elem).data('draggable')) {
        alert("yes");
}
else {
        alert("no");
}

if ($(elem).data('fooable')) {
        alert("yes");
}
else {
        alert("no");
} 

See it here: http://bootply.com/60153

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
15

This works for me with JQuery 1.10.2

if ($("el").data('uiDraggable')){ //or uiDroppable
   alert("draggable")
} else {
   alert("not draggable")
}

Alternatively it is possible to invoke .data() method without argument

$("el").data()

That should print something like

Object {uiDraggable: $.(anonymous function).(anonymous function)}

where you can see object properties.

humkins
  • 9,635
  • 11
  • 57
  • 75
7

For draggable elements:

$(elem).is('.ui-draggable')

or you could filter, or just select $('.ui-draggable');.

For droppable, you would use .ui-droppable, resizable is .ui-resizable, selectable is .ui-selectable for the container although the items you select are .ui-selectee, sortable is .ui-sortable for the container.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • What is better? Css class checking or checking in the `data`? And why? – Kees C. Bakker Nov 21 '11 at 20:50
  • That really depends on your implementation. It's very easy to remove a class, it's also easy to remove some data. I'm getting the feeling that this question is a symptom of using the wrong approach. – zzzzBov Nov 21 '11 at 21:25
0

I use Modernizr:

if (Modernizr.draganddrop) {
// use drag and drop
}
Vasyl Gutnyk
  • 4,813
  • 2
  • 34
  • 37
0

Just check for the classes added by jQuery.

if ($(this).hasClass('ui-droppable')) {
  // Is Droppable
} else {
  // Nope
}