How To Get Element Order Using jQuery Sortable

If you are using the sortable feature of the jQuery library, you will likely want a way to get the order of the elements. You can do this very easily using one of the built in events. The following code will obtain the element order for a sortable div and output the data to the console.

<ul id="#sortable">
	<li data-id="1">Item 1</li>
	<li data-id="2">Item 2</li>
</ul>

$( "#sortable" ).sortable(
{
	stop: function( event, ui ) 
	{
		var idsInOrder = $("#visualpreview").sortable('toArray', { attribute: 'data-id' });
		console.log(idsInOrder);
	}
});

The output will be a simple numeric array e.g. [1,2]. You can loop over this array and do what you need to do. You can also save these to a comma delimited string if you need to submit the order of a sortable list through a form.

Related Articles

Related Questions

Why Can’t I Find Realtek Audio on My Windows 11 PC?

I'm having trouble with my new PC. I checked the Device Manager under 'Sound, video and game controllers,' but I can't find the Realtek...

Should I Create a Game or an App for My First Project?

Hey everyone! I'm nearly 16 and super eager to dive into my first "real" project. I'm torn between making a game or an app...

Can I Use a WD Passport SSD with My HP Omen?

I'm dealing with a situation where the SSD in my HP Omen has become corrupted. In my haste to replace it, I bought a...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Tools

Online Hash Generator – String to Hash Converter

Need to quickly generate a hash from a string? Whether you're verifying file integrity, securing data, or just experimenting with cryptographic tools, this simple...

Convert CSV To HTML Table

Need to quickly turn CSV data into an HTML table? Whether you're copying data from Excel, Google Sheets, or another spreadsheet, this tool makes...

Student Group Randomizer

Creating fair and balanced groups in the classroom can be time-consuming — especially when you're trying to avoid repetition, manage different skill levels, or...

Random Group Generator

Need to split a list of people, items, or ideas into random groups? Our free Random Group Generator makes it quick and easy. Whether...

Flip Text Upside Down – Free Online Tool

Ever wanted to flip your text upside down just for fun or to grab someone’s attention in a creative way? This free online Upside...

Raffle Ticket Generator

If you're running a fundraiser, charity draw, or local event and need raffle tickets fast, this free online tool lets you generate and print...

Latest Posts

Latest Questions