script.aculo.us Sorting tree Option
Last Updated :
23 Jul, 2025
The user should have the ability to reorder elements (such as items in a list) by dragging and dropping them. Without drag and drop, it is not an ordinary task to reorder but script.aculo.us provides extended reordering support out of the box through the Sortable class.
When the tree option is set to "true", it enables sorting with sub-elements within the sortable element. The default value is "false". The element to make it sortable is passed to the create() method in the sortable namespace.
A Sortable consists of item elements in a container element. When you create a new Sortable, it takes care of the creation of the corresponding draggables and dropables.
To use script.aculo.us sortable capabilities, you'll need to load the drag drop module, which also requires the effects module. The pre-compiled files required for code implementation are as follows.
<script type="text/javascript" src="scriptaculous-js-1.9.0/lib/prototype.js"></script>
<script type="text/javascript" src="scriptaculous-js-1.9.0/src/scriptaculous.js?load = effects,dragdrop">
Syntax: The create() method is used to create a sortable item. The create() method takes the id of the container element and sorts them out based on the options which are passed.
Sortable.create('id-of-container', [options]);
Sortable.destroy() method is used to remove all the event handlers and references created by Sortable.create() method.
A call to Sortable.create, implicitly calls on Sortable.destroy if the referenced element was already Sortable.
Syntax:
Sortable.destroy( element );
Example 1: The following demonstrates the drag and drop feature for a list. The next example will demonstrate the drag and drop for a tree which is developed on top of a simple list element.
HTML
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="prototype.js">
</script>
<script type="text/javascript" src=
"scriptaculous.js?load = effects,dragdrop">
</script>
<script type="text/javascript ">
window.onload = function() {
Sortable.create('namelist', {
tag: 'li'
});
}
</script>
<style type="text/css ">
li {
cursor: move;
}
</style>
</head>
<body>
<p>
Drag and drop the elements
that are to be sorted
</p>
<ul id="namelist ">
<li>Spanish</li>
<li>French</li>
<li>English</li>
<li>Russian</li>
<li>Arab</li>
<li>Chinese</li>
<li>Portugese</li>
<li>Turkish</li>
</ul>
</body>
</html>
Output:
Before execute:
After execute:
Example 2: The following example demonstrates the drag and drops for a tree structure created out of list elements.
HTML
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript"
src="prototype.js">
</script>
<script type="text/javascript" src=
"scriptaculous.js?load = effects,dragdrop" ">
</script>
<script type="text/javascript ">
window.onload = function() {
Sortable.create('namelist', {
tree: true,
scroll: window,
treeTag: 'ul',
tag: 'li'
});
}
</script>
<style type = "text/css ">
li { cursor: move; }
</style>
</head>
<body>
<p>
Drag and drop the elements
that are to be sorted
</p>
<ul id="namelist ">
<li>Web Technologies
<ul>
<li>PHP</li>
<li>CSS</li>
<li>JavaScript</li>
</ul>
</li>
<li>Computer Science
<ul>
<li>Data structures</li>
<li>Theory of CS</li>
<li>Algorithms</li>
</ul>
</li>
<li>English
<ul>
<li>Grammar</li>
<li>Literature</li>
</ul>
</li>
<li>Maths</li>
<li>Social studies</li>
</ul>
</body>
</html>
Output:
Before execute:
After execute:
Similar Reads
script.aculo.us Sorting tag Option The tag option in the Sortable module is used to specify the type of elements that have to be made sortable in the container. Its default value is 'li', which is the list element. Syntax: Sortable.create( container_id, {tag: container_type} ); Example 1: HTML <!DOCTYPE html> <html> <h
1 min read
script.aculo.us Sorting overlap Option The script.aculo.us library is a cross-browser library that aims at improving the user interface of a website. The sortable module can be used to make any list sortable, allowing the user to drag any item according to the order required. The overlap option is used to specify whether the item in the
3 min read
script.aculo.us Sorting ghosting Option The ghosting option in the Sortable module is used to enable the user to make a semi-transparent copy of the element that is moved along with the mouse pointer. Its default value is 'falseâ, which means no copy is made. Syntax: Sortable.create('list', {ghosting: boolean}) The examples below demonstr
1 min read
script.aculo.us Sorting constraint Option The constraint option in the Sortable module is used to restrict the direction of movement of the elements while they are being dragged. It can either be set to 'horizontal' or 'vertical', thereby allowing movement in only that direction. Its default value is 'verticalâ. Syntax: Sortable.create('lis
1 min read
script.aculo.us Sorting dropOnEmpty Option The dropOnEmpy option in the Sortable module allows sortable elements to be dropped onto another list. Its default value is false. Syntax: Sortable.create(List1_id, {containment: [List1_id, List2_id], dropOnEmpty: true}); Sortable.create(List2_id, {containment: [List1_id, List2_id], dropOnEmpty: tru
1 min read
script.aculo.us Sorting Elements Sortable provides the ability to user to create many draggable elements within the container. When you create a Sortable, it automatically creates the corresponding Draggable and Droppable for the container. Syntax:Â Â Sortable.create('container_id', {options}); Sortable Options: OptionsDescriptionta
2 min read