Sortable Widget | jQuery UI API Documentation (original) (raw)

Description: Reorder elements in a list or grid using the mouse.

QuickNavExamples

Options

Methods

Events

The jQuery UI Sortable plugin makes selected elements sortable by dragging with the mouse.

Note: In order to sort table rows, the tbody must be made sortable, not the table.

Theming

The sortable widget uses the jQuery UI CSS framework to style its look and feel. If sortable specific styling is needed, the following CSS class names can be used for overrides or as keys for the classes option:

Dependencies

Options

appendTo

Default: "parent"

Defines where the helper that moves with the mouse is being appended to during the drag (for example, to resolve overlap/zIndex issues).

Multiple types supported:

Code examples:

Initialize the sortable with the appendTo option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the appendTo option, after initialization:

1 2 3 4 5 var appendTo = (".selector").sortable("option","appendTo");( ".selector" ).sortable( "option", "appendTo" );(".selector").sortable("option","appendTo");( ".selector" ).sortable( "option", "appendTo", document.body );

axis

Default: false

If defined, the items can be dragged only horizontally or vertically. Possible values: "x", "y".

Code examples:

Initialize the sortable with the axis option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the axis option, after initialization:

1 2 3 4 5 var axis = (".selector").sortable("option","axis");( ".selector" ).sortable( "option", "axis" );(".selector").sortable("option","axis");( ".selector" ).sortable( "option", "axis", "x" );

cancel

Default: "input,textarea,button,select,option"

Prevents sorting if you start on elements matching the selector.

Code examples:

Initialize the sortable with the cancel option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the cancel option, after initialization:

1 2 3 4 5 var cancel = (".selector").sortable("option","cancel");( ".selector" ).sortable( "option", "cancel" );(".selector").sortable("option","cancel");( ".selector" ).sortable( "option", "cancel", "a,button" );

classes

Default: {}

Specify additional classes to add to the widget's elements. Any of classes specified in the Theming section can be used as keys to override their value. To learn more about this option, check out the learn article about the classes option.

Code examples:

Initialize the sortable with the classes option specified, changing the theming for the ui-sortable class:

1 2 3 4 5 $( ".selector" ).sortable({ "ui-sortable": "highlight"

Get or set a property of the classes option, after initialization, here reading and changing the theming for the ui-sortable class:

1 2 3 4 5 var themeClass = (".selector").sortable("option","classes.ui−sortable");( ".selector" ).sortable( "option", "classes.ui-sortable" );(".selector").sortable("option","classes.uisortable");( ".selector" ).sortable( "option", "classes.ui-sortable", "highlight" );

connectWith

Default: false

A selector of other sortable elements that the items from this list should be connected to. This is a one-way relationship, if you want the items to be connected in both directions, the connectWith option must be set on both sortable elements.

Code examples:

Initialize the sortable with the connectWith option specified:

1 2 3 $( ".selector" ).sortable({ connectWith: "#shopping-cart"

Get or set the connectWith option, after initialization:

1 2 3 4 5 var connectWith = (".selector").sortable("option","connectWith");( ".selector" ).sortable( "option", "connectWith" );(".selector").sortable("option","connectWith");( ".selector" ).sortable( "option", "connectWith", "#shopping-cart" );

containment

Default: false

Defines a bounding box that the sortable items are constrained to while dragging.

Note: The element specified for containment must have a calculated width and height (though it need not be explicit). For example, if you have float: left sortable children and specify containment: "parent" be sure to have float: left on the sortable/parent container as well or it will have height: 0, causing undefined behavior.

Multiple types supported:

Code examples:

Initialize the sortable with the containment option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the containment option, after initialization:

1 2 3 4 5 var containment = (".selector").sortable("option","containment");( ".selector" ).sortable( "option", "containment" );(".selector").sortable("option","containment");( ".selector" ).sortable( "option", "containment", "parent" );

cursor

Default: "auto"

Defines the cursor that is being shown while sorting.

Code examples:

Initialize the sortable with the cursor option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the cursor option, after initialization:

1 2 3 4 5 var cursor = (".selector").sortable("option","cursor");( ".selector" ).sortable( "option", "cursor" );(".selector").sortable("option","cursor");( ".selector" ).sortable( "option", "cursor", "move" );

cursorAt

Default: false

Moves the sorting element or helper so the cursor always appears to drag from the same position. Coordinates can be given as a hash using a combination of one or two keys: { top, left, right, bottom }.

Code examples:

Initialize the sortable with the cursorAt option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the cursorAt option, after initialization:

1 2 3 4 5 var cursorAt = (".selector").sortable("option","cursorAt");( ".selector" ).sortable( "option", "cursorAt" );(".selector").sortable("option","cursorAt");( ".selector" ).sortable( "option", "cursorAt", { left: 5 } );

delay

Default: 0

Time in milliseconds to define when the sorting should start. Adding a delay helps preventing unwanted drags when clicking on an element. (version deprecated: 1.12)

Code examples:

Initialize the sortable with the delay option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the delay option, after initialization:

1 2 3 4 5 var delay = (".selector").sortable("option","delay");( ".selector" ).sortable( "option", "delay" );(".selector").sortable("option","delay");( ".selector" ).sortable( "option", "delay", 150 );

disabled

Default: false

Disables the sortable if set to true.

Code examples:

Initialize the sortable with the disabled option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the disabled option, after initialization:

1 2 3 4 5 var disabled = (".selector").sortable("option","disabled");( ".selector" ).sortable( "option", "disabled" );(".selector").sortable("option","disabled");( ".selector" ).sortable( "option", "disabled", true );

distance

Default: 1

Tolerance, in pixels, for when sorting should start. If specified, sorting will not start until after mouse is dragged beyond distance. Can be used to allow for clicks on elements within a handle. (version deprecated: 1.12)

Code examples:

Initialize the sortable with the distance option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the distance option, after initialization:

1 2 3 4 5 var distance = (".selector").sortable("option","distance");( ".selector" ).sortable( "option", "distance" );(".selector").sortable("option","distance");( ".selector" ).sortable( "option", "distance", 5 );

dropOnEmpty

Default: true

If false, items from this sortable can't be dropped on an empty connect sortable (see the connectWith option.

Code examples:

Initialize the sortable with the dropOnEmpty option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the dropOnEmpty option, after initialization:

1 2 3 4 5 var dropOnEmpty = (".selector").sortable("option","dropOnEmpty");( ".selector" ).sortable( "option", "dropOnEmpty" );(".selector").sortable("option","dropOnEmpty");( ".selector" ).sortable( "option", "dropOnEmpty", false );

forceHelperSize

Default: false

If true, forces the helper to have a size.

Code examples:

Initialize the sortable with the forceHelperSize option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the forceHelperSize option, after initialization:

1 2 3 4 5 var forceHelperSize = (".selector").sortable("option","forceHelperSize");( ".selector" ).sortable( "option", "forceHelperSize" );(".selector").sortable("option","forceHelperSize");( ".selector" ).sortable( "option", "forceHelperSize", true );

forcePlaceholderSize

Default: false

If true, forces the placeholder to have a size.

Code examples:

Initialize the sortable with the forcePlaceholderSize option specified:

1 2 3 $( ".selector" ).sortable({ forcePlaceholderSize: true

Get or set the forcePlaceholderSize option, after initialization:

1 2 3 4 5 var forcePlaceholderSize = (".selector").sortable("option","forcePlaceholderSize");( ".selector" ).sortable( "option", "forcePlaceholderSize" );(".selector").sortable("option","forcePlaceholderSize");( ".selector" ).sortable( "option", "forcePlaceholderSize", true );

grid

Default: false

Snaps the sorting element or helper to a grid, every x and y pixels. Array values: [ x, y ].

Code examples:

Initialize the sortable with the grid option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the grid option, after initialization:

1 2 3 4 5 var grid = (".selector").sortable("option","grid");( ".selector" ).sortable( "option", "grid" );(".selector").sortable("option","grid");( ".selector" ).sortable( "option", "grid", [ 20, 10 ] );

handle

Default: false

Restricts sort start click to the specified element.

Code examples:

Initialize the sortable with the handle option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the handle option, after initialization:

1 2 3 4 5 var handle = (".selector").sortable("option","handle");( ".selector" ).sortable( "option", "handle" );(".selector").sortable("option","handle");( ".selector" ).sortable( "option", "handle", ".handle" );

helper

Default: "original"

Allows for a helper element to be used for dragging display.

Multiple types supported:

Code examples:

Initialize the sortable with the helper option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the helper option, after initialization:

1 2 3 4 5 var helper = (".selector").sortable("option","helper");( ".selector" ).sortable( "option", "helper" );(".selector").sortable("option","helper");( ".selector" ).sortable( "option", "helper", "clone" );

items

Default: "> *"

Specifies which items inside the element should be sortable.

Code examples:

Initialize the sortable with the items option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the items option, after initialization:

1 2 3 4 5 var items = (".selector").sortable("option","items");( ".selector" ).sortable( "option", "items" );(".selector").sortable("option","items");( ".selector" ).sortable( "option", "items", "> li" );

opacity

Default: false

Defines the opacity of the helper while sorting. From 0.01 to 1.

Code examples:

Initialize the sortable with the opacity option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the opacity option, after initialization:

1 2 3 4 5 var opacity = (".selector").sortable("option","opacity");( ".selector" ).sortable( "option", "opacity" );(".selector").sortable("option","opacity");( ".selector" ).sortable( "option", "opacity", 0.5 );

placeholder

Default: false

A class name that gets applied to the otherwise white space.

Code examples:

Initialize the sortable with the placeholder option specified:

1 2 3 $( ".selector" ).sortable({ placeholder: "sortable-placeholder"

Get or set the placeholder option, after initialization:

1 2 3 4 5 var placeholder = (".selector").sortable("option","placeholder");( ".selector" ).sortable( "option", "placeholder" );(".selector").sortable("option","placeholder");( ".selector" ).sortable( "option", "placeholder", "sortable-placeholder" );

revert

Default: false

Whether the sortable items should revert to their new positions using a smooth animation.

Multiple types supported:

Code examples:

Initialize the sortable with the revert option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the revert option, after initialization:

1 2 3 4 5 var revert = (".selector").sortable("option","revert");( ".selector" ).sortable( "option", "revert" );(".selector").sortable("option","revert");( ".selector" ).sortable( "option", "revert", true );

tolerance

Default: "intersect"

Specifies which mode to use for testing whether the item being moved is hovering over another item. Possible values:

Code examples:

Initialize the sortable with the tolerance option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the tolerance option, after initialization:

1 2 3 4 5 var tolerance = (".selector").sortable("option","tolerance");( ".selector" ).sortable( "option", "tolerance" );(".selector").sortable("option","tolerance");( ".selector" ).sortable( "option", "tolerance", "pointer" );

zIndex

Default: 1000

Z-index for element/helper while being sorted.

Code examples:

Initialize the sortable with the zIndex option specified:

1 2 3 $( ".selector" ).sortable({

Get or set the zIndex option, after initialization:

1 2 3 4 5 var zIndex = (".selector").sortable("option","zIndex");( ".selector" ).sortable( "option", "zIndex" );(".selector").sortable("option","zIndex");( ".selector" ).sortable( "option", "zIndex", 9999 );

Methods

cancel()Returns: jQuery (plugin only)

Cancels a change in the current sortable and reverts it to the state prior to when the current sort was started. Useful in the stop and receive callback functions.

Code examples:

Invoke the cancel method:

1 $( ".selector" ).sortable( "cancel" );

destroy()Returns: jQuery (plugin only)

Removes the sortable functionality completely. This will return the element back to its pre-init state.

Code examples:

Invoke the destroy method:

1 $( ".selector" ).sortable( "destroy" );

disable()Returns: jQuery (plugin only)

Disables the sortable.

Code examples:

Invoke the disable method:

1 $( ".selector" ).sortable( "disable" );

enable()Returns: jQuery (plugin only)

Enables the sortable.

Code examples:

Invoke the enable method:

1 $( ".selector" ).sortable( "enable" );

instance()Returns: Object

Retrieves the sortable's instance object. If the element does not have an associated instance, undefined is returned.

Unlike other widget methods, instance() is safe to call on any element after the sortable plugin has loaded.

Code examples:

Invoke the instance method:

1 $( ".selector" ).sortable( "instance" );

option( optionName )Returns: Object

Gets the value currently associated with the specified optionName.

Note: For options that have objects as their value, you can get the value of a specific key by using dot notation. For example, "foo.bar" would get the value of the bar property on the foo option.

Code examples:

Invoke the method:

1 var isDisabled = $( ".selector" ).sortable( "option", "disabled" );

option()Returns: PlainObject

Gets an object containing key/value pairs representing the current sortable options hash.

Code examples:

Invoke the method:

1 var options = $( ".selector" ).sortable( "option" );

option( optionName, value )Returns: jQuery (plugin only)

Sets the value of the sortable option associated with the specified optionName.

Note: For options that have objects as their value, you can set the value of just one property by using dot notation for optionName. For example, "foo.bar" would update only the bar property of the foo option.

Code examples:

Invoke the method:

1 $( ".selector" ).sortable( "option", "disabled", true );

option( options )Returns: jQuery (plugin only)

Sets one or more options for the sortable.

Code examples:

Invoke the method:

1 $( ".selector" ).sortable( "option", { disabled: true } );

refresh()Returns: jQuery (plugin only)

Refresh the sortable items. Triggers the reloading of all sortable items, causing new items to be recognized.

Code examples:

Invoke the refresh method:

1 $( ".selector" ).sortable( "refresh" );

refreshPositions()Returns: jQuery (plugin only)

Refresh the cached positions of the sortable items. Calling this method refreshes the cached item positions of all sortables.

Code examples:

Invoke the refreshPositions method:

1 $( ".selector" ).sortable( "refreshPositions" );

serialize( options )Returns: String

Serializes the sortable's item ids into a form/ajax submittable string. Calling this method produces a hash that can be appended to any url to easily submit a new item order back to the server.

It works by default by looking at the id of each item in the format "setname_number", and it spits out a hash like "setname[]=number&setname[]=number".

Note: If serialize returns an empty string, make sure the id attributes include an underscore. They must be in the form: "setnumber" For example, a 3 element list with id attributes "foo1", "foo5", "foo2" will serialize to "foo[]=1&foo[]=5&foo[]=2". You can use an underscore, equal sign or hyphen to separate the set and number. For example "foo=1", "foo-1", and "foo1" all serialize to "foo[]=1".

Code examples:

Invoke the serialize method:

1 var sorted = $( ".selector" ).sortable( "serialize", { key: "sort" } );

toArray( options )Returns: Array

Serializes the sortable's item id's into an array of string.

Code examples:

Invoke the toArray method:

1 var sortedIDs = $( ".selector" ).sortable( "toArray" );

Events

activate( event, ui )Type: sortactivate

This event is triggered when using connected lists, every connected list on drag start receives it.

Code examples:

Initialize the sortable with the activate callback specified:

1 2 3 $( ".selector" ).sortable({ activate: function( event, ui ) {}

Bind an event listener to the sortactivate event:

1 $( ".selector" ).on( "sortactivate", function( event, ui ) {} );

beforeStop( event, ui )Type: sortbeforestop

This event is triggered when sorting stops, but when the placeholder/helper is still available.

Code examples:

Initialize the sortable with the beforeStop callback specified:

1 2 3 $( ".selector" ).sortable({ beforeStop: function( event, ui ) {}

Bind an event listener to the sortbeforestop event:

1 $( ".selector" ).on( "sortbeforestop", function( event, ui ) {} );

change( event, ui )Type: sortchange

This event is triggered during sorting, but only when the DOM position has changed.

Code examples:

Initialize the sortable with the change callback specified:

1 2 3 $( ".selector" ).sortable({ change: function( event, ui ) {}

Bind an event listener to the sortchange event:

1 $( ".selector" ).on( "sortchange", function( event, ui ) {} );

create( event, ui )Type: sortcreate

Triggered when the sortable is created.

Note: The ui object is empty but included for consistency with other events.

Code examples:

Initialize the sortable with the create callback specified:

1 2 3 $( ".selector" ).sortable({ create: function( event, ui ) {}

Bind an event listener to the sortcreate event:

1 $( ".selector" ).on( "sortcreate", function( event, ui ) {} );

deactivate( event, ui )Type: sortdeactivate

This event is triggered when sorting was stopped, is propagated to all possible connected lists.

Code examples:

Initialize the sortable with the deactivate callback specified:

1 2 3 $( ".selector" ).sortable({ deactivate: function( event, ui ) {}

Bind an event listener to the sortdeactivate event:

1 $( ".selector" ).on( "sortdeactivate", function( event, ui ) {} );

out( event, ui )Type: sortout

This event is triggered when a sortable item is moved away from a sortable list.

Note: This event is also triggered when a sortable item is dropped.

Code examples:

Initialize the sortable with the out callback specified:

1 2 3 $( ".selector" ).sortable({ out: function( event, ui ) {}

Bind an event listener to the sortout event:

1 $( ".selector" ).on( "sortout", function( event, ui ) {} );

over( event, ui )Type: sortover

This event is triggered when a sortable item is moved into a sortable list.

Code examples:

Initialize the sortable with the over callback specified:

1 2 3 $( ".selector" ).sortable({ over: function( event, ui ) {}

Bind an event listener to the sortover event:

1 $( ".selector" ).on( "sortover", function( event, ui ) {} );

receive( event, ui )Type: sortreceive

This event is triggered when an item from a connected sortable list has been dropped into another list. The latter is the event target.

Code examples:

Initialize the sortable with the receive callback specified:

1 2 3 $( ".selector" ).sortable({ receive: function( event, ui ) {}

Bind an event listener to the sortreceive event:

1 $( ".selector" ).on( "sortreceive", function( event, ui ) {} );

remove( event, ui )Type: sortremove

This event is triggered when a sortable item from the list has been dropped into another. The former is the event target.

Code examples:

Initialize the sortable with the remove callback specified:

1 2 3 $( ".selector" ).sortable({ remove: function( event, ui ) {}

Bind an event listener to the sortremove event:

1 $( ".selector" ).on( "sortremove", function( event, ui ) {} );

sort( event, ui )Type: sort

This event is triggered during sorting.

Code examples:

Initialize the sortable with the sort callback specified:

1 2 3 $( ".selector" ).sortable({ sort: function( event, ui ) {}

Bind an event listener to the sort event:

1 $( ".selector" ).on( "sort", function( event, ui ) {} );

start( event, ui )Type: sortstart

This event is triggered when sorting starts.

Code examples:

Initialize the sortable with the start callback specified:

1 2 3 $( ".selector" ).sortable({ start: function( event, ui ) {}

Bind an event listener to the sortstart event:

1 $( ".selector" ).on( "sortstart", function( event, ui ) {} );

stop( event, ui )Type: sortstop

This event is triggered when sorting has stopped.

Code examples:

Initialize the sortable with the stop callback specified:

1 2 3 $( ".selector" ).sortable({ stop: function( event, ui ) {}

Bind an event listener to the sortstop event:

1 $( ".selector" ).on( "sortstop", function( event, ui ) {} );

update( event, ui )Type: sortupdate

This event is triggered when the user stopped sorting and the DOM position has changed.

Code examples:

Initialize the sortable with the update callback specified:

1 2 3 $( ".selector" ).sortable({ update: function( event, ui ) {}

Bind an event listener to the sortupdate event:

1 $( ".selector" ).on( "sortupdate", function( event, ui ) {} );

Example:

A simple jQuery UI Sortable.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <title>sortable demo</title> <link rel="stylesheet" href="" title="undefined" rel="noopener noreferrer">https://code.jquery.com/ui/1.13.3/themes/smoothness/jquery-ui.css"\> <script src="" title="undefined" rel="noopener noreferrer">https://code.jquery.com/jquery-3.7.1.js"\></script> <script src="" title="undefined" rel="noopener noreferrer">https://code.jquery.com/ui/1.13.3/jquery-ui.js"\></script><script>$("#sortable").sortable();</script>

Demo: