Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 56 ยท Plugins and Utilities

jQuery UI Interactions: Draggable, Droppable, Resizable, Selectable, Sortable

Learn the five jQuery UI interactions: draggable, droppable, resizable, selectable and sortable, with options, events, methods and a working drag-and-drop example.

jQuery UI has two families of building blocks. Widgets are finished interface parts such as dialogs and date pickers. Interactions are behaviours you add to elements you already have: make a box draggable, let a list be re-ordered, let the user lasso several items. They use the mouse (and touch with a small helper), and they are what many old-school dashboards and kanban boards were built from.

There are five interactions: draggable, droppable, resizable, selectable and sortable. They share the same API shape as every jQuery UI component, which you met in Using Plugins and jQuery UI: .name(options) creates, .name("method", args) calls a method, .name("option", key, value) changes an option, and events arrive as callbacks or as events named namestart, namestop and so on.

The five interactions

Draggable Move an element with the pointer.
Droppable A target that reacts to a draggable.
Resizable Drag handles change the size.
Selectable Lasso or click to select items.
1 / 2

Every example here loads jQuery, then jQuery UI, then the base theme stylesheet. The JavaScript file brings all interactions; the stylesheet is only needed for a few visual classes such as the selection highlight.

Draggable

.draggable() lets the user move an element. While the pointer is down, jQuery UI changes the element's top and left, so the element must be positioned (the plugin sets position: relative for you if it is static).

The options you will use most:

Option What it does
axis "x" or "y" restricts movement to one direction
containment "parent", "window", a selector or [x1, y1, x2, y2] keeps the element inside an area
grid [20, 20] snaps movement to steps in pixels
handle a selector for the part of the element that starts a drag
cancel elements that must not start a drag (default: inputs, buttons, links)
helper "original" (default), "clone" or a function that returns an element to drag instead
revert true, "invalid" or a function: animate back to the start position
snap snap to the edges of other elements
opacity, cursor, zIndex cosmetic settings while dragging

Events: create, start, drag and stop. Each callback receives (event, ui), where ui.position is the current { top, left } and ui.helper is the element being moved.

Methods: "destroy", "disable", "enable", "option" and "widget".

Droppable

.droppable() turns an element into a target that notices when a draggable is moved over it or released on it. Droppable by itself does nothing visible: you tell it what to accept and what to do through events.

  • accept is a selector (or function) that limits which draggables count.
  • tolerance decides what "over" means: "fit", "intersect", "pointer" or "touch".
  • classes lets you add CSS classes when a draggable is active (ui-droppable-active) or hovering (ui-droppable-hover).
  • Events: activate, deactivate, over, out and drop. In drop, ui.draggable is the element that was released.
Drag a card into a drop zone

Drag a blue card into the zone and it stays; drag the red one and it springs back, because revert: "invalid" returns a draggable that was not accepted by any droppable. Inside drop we re-parent the element and clear the offsets that dragging added, which is the usual way to make a drop "stick".

Resizable

.resizable() adds drag handles so the user can change an element's size. Useful options are handles ("e, s, se" by default, or "all"), minWidth, maxWidth, minHeight, maxHeight, aspectRatio (true or a ratio), grid, containment, animate and ghost (resize an outline and apply it on release). Events are start, resize and stop, with ui.size holding the new { width, height }.

Resizable adds the handles as child elements, so it does not work on elements that cannot have children, such as <img>, <textarea> or <canvas>. Wrap those in a <div> and make the wrapper resizable (jQuery UI can also do it with the alsoResize option).

Selectable

.selectable() lets users select items by clicking, Ctrl/Cmd-clicking, or dragging a lasso over a group, like files in a file manager. It adds ui-selecting while the lasso touches an item and ui-selected once it is chosen. Style those classes yourself.

Options: filter (which children are selectable, default "*"), cancel, distance, tolerance ("touch" or "fit"). Events: start, selecting, unselecting, selected, unselected and stop. The stop event runs once, after the user finishes the gesture, which is the right place to read the final selection with $(".ui-selected", this).

Sortable

.sortable() makes the children of a container re-orderable. It is the interaction used most in real projects (to-do lists, menu builders, kanban columns).

Key options:

  • items: a selector for what can be moved (default "> *").
  • handle: restrict the drag start to a grip icon.
  • placeholder: a class for the gap that shows where the item will land.
  • connectWith: a selector for other sortable lists the items may move to.
  • axis, containment, cursor, opacity, tolerance, revert, cancel.

Events: start, sort, change (position changed during a drag), update (the order really changed and the drag ended), receive (an item arrived from another list), remove, stop.

Methods worth knowing: "toArray" returns the item ids in order, "serialize" returns a query string like item[]=3&item[]=1 ready to send to a server, and "cancel" aborts a drag in progress.

Two connected sortable lists

The update callback fires on both lists when an item moves between them, so report runs twice for a cross-list move; that is harmless here, but when you save to a server, check ui.sender or debounce so you do not post twice.

Touch devices

jQuery UI interactions are built on mouse events. On phones and tablets nothing happens unless you add the small jQuery UI Touch Punch shim, which converts touch events into mouse events, or use a library designed for touch from the start. That is one more reason to look at SortableJS or the native Drag and Drop API for new work, as discussed in Using Plugins and jQuery UI. Whatever you choose, always offer a keyboard alternative (for example "Move up" and "Move down" buttons), because dragging excludes keyboard and many assistive technology users.

Cleaning up

Interactions attach classes, handlers and sometimes helper elements. Remove them with .draggable("destroy") (or the matching method) before deleting or re-initialising an element, or use .disable() and .enable() to switch them off temporarily. Changing an option later is as simple as .sortable("option", "axis", "y").

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which option makes a draggable element spring back to its starting position when it is not dropped on an accepting target?

    revert: "invalid" animates the element back when the drop is not accepted by any droppable. revert: true always reverts.
  2. 2. Which sortable event is the right place to save the new order, because it only fires when the order really changed?

    update fires after the user stops dragging and the position in the list has changed. sort fires continuously while dragging.
  3. 3. What does $("#list").sortable("serialize") return?

    serialize builds a form-style string from ids such as item_1, ready to send to a server. toArray returns the ids as an array.
  4. 4. Why does $("img").resizable() not work reliably?

    Wrap the image in a div and make the wrapper resizable, letting the image fill it.
  5. 5. How do you let items move between two sortable lists?

    connectWith links the lists so items can be dragged from one to the other; receive and remove events tell each list what happened.

Summary

  • jQuery UI offers five interactions: draggable, droppable, resizable, selectable and sortable.
  • Draggable moves; droppable reacts through accept, over, out and drop; use revert: "invalid" for forgiving drags.
  • Resizable needs an element that can have children; selectable marks items with ui-selected.
  • Sortable supports connectWith, toArray, serialize and the update event for saving a new order.
  • Interactions are mouse-based, so add a touch shim or a keyboard alternative, and call destroy when you remove elements.

Next: form-style widgets in jQuery UI Form Widgets.