jqueryHow do I use jQuery UI Sortable to arrange elements on a webpage?
jQuery UI Sortable is a jQuery plugin that allows users to rearrange elements on a webpage by dragging and dropping them. To use it, you must include the jQuery and jQuery UI libraries in your HTML file:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
You must also specify the sortable()
method on the jQuery object you wish to sort. For example, to make a list of items sortable:
$('#myList').sortable();
The sortable()
method takes a number of options to customize the sortable behavior. For example, you can specify the axis
option to restrict sorting to either the x-axis or y-axis:
$('#myList').sortable({axis: 'y'});
You can also specify the handle
option to specify which elements can be used to drag and drop the items:
$('#myList').sortable({handle: '.my-handle'});
Finally, you can specify callback functions to be executed when the user starts, stops, or changes the order of the elements. For example, to log the new order of the elements to the console when the user changes the order:
$('#myList').sortable({
change: function( event, ui ) {
console.log($(this).sortable('toArray'));
}
});
The output of the above code would be an array of the elements in the new order.
Helpful links
More of Jquery
- How do I use jQuery to zip files?
- How do I use jQuery to zoom in or out on an element?
- How can I use JQuery with Yii2?
- How do I use a jQuery x-csrf-token?
- How do I use jQuery Knob to customize user input?
- How can I use jQuery to access data attributes?
- How do I use jQuery ZTree to create a hierarchical tree structure?
- How do I download a zip file using jQuery?
- How do I create a quiz using the jQuery plugin?
- How can I use jQuery without a class?
See more codes...