Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4082842
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:14:16+00:00 2026-05-20T18:14:16+00:00

I have a list on my website. I’m using jQuery’s sortable tutorial to give

  • 0

I have a list on my website. I’m using jQuery’s sortable tutorial to give users the ability to change the order of the list items.

http://jqueryui.com/demos/sortable/

The trick is I would like to capture the order of the items immediately after a resort and assign the order values to hidden form elements which would be passed to my server via a form-submit where I could use a php script to save the new order of elements in a database.

Here’s the source code of the demo:

 <style>
    #sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
    #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
    #sortable li span { position: absolute; margin-left: -1.3em; }
    </style>
    <script>
    $(function() {
        $( "#sortable" ).sortable();
        $( "#sortable" ).disableSelection();
    });
    </script>


<div class="demo">

<ul id="sortable">
    <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 1</li>
    <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 2</li>
    <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 3</li>
    <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 4</li>
    <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 5</li>
    <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 6</li>
    <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 7</li>
</ul>

</div><!-- End demo -->

And I’m aware that it’s also possible to assign a call-back function that fires when sorting stops:

$( ".selector" ).sortable({
   stop: function(event, ui) { ... }
});

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:14:16+00:00Added an answer on May 20, 2026 at 6:14 pm

    I wrote an answer to this question 5 years ago, but that answer sucked (and this question has almost 38,000 views), so here’s an improved answer.

    There’s essentially three parts of this question that you have to solve. We’ll look at all three.

    Responding to changes in the sort order (Step 1)

    The first issue we need to solve is reacting to changes in the order of sorted elements. If we check out the jQuery UI Sortable Widget’s documentation, we see that it has a change event which fires whenever the sort order changes, and is perfect for our needs.

    Side note: My original answer used stop instead of the change event. change is better (at least in this case) because it will report all changes in sorting, whether the change was interactive (user) or programmatic, and only if the order has actually changed. On the other hand, the sort event is only fired when the user stops sorting (releases the mouse, or lifts their finger).

    Using the sort event, we can now respond to changes in sorting. The following will initialize a Sortable widget for us, and allow us to set a function to be called when the sort even fires:

    var $sortableList = $("#your-list");
    
    var sortEventHandler = function(event, ui){
        console.log("New sort order!");
    };
    
    $sortableList.sortable({
        stop: sortEventHandler
    });
    
    // You can also set the event handler on an already existing Sortable widget this way:
    
    $sortableList.on("sortchange", sortEventHandler);
    

    With that done, we’re now ready to take on step 2:

    Retrieving the sorted elements (Step 2)

    This part is fairly simple. We just need to get an array of the elements in our sorted list. To do this, we can just ask for the children of the ul (list) element, using the jQuery function children():

    var listElements = $sortableList.children();
    
    console.log(listElements); // [ <li>, <li>, ... ]
    

    Great, but we specifically need the element’s values:

    var listValues = [];
    
    listElement.forEach(function(element){
        listValues.push(element.innerHTML);
    });
    
    console.log(listValues); // [ "Item 1", "Item 2", ... ]
    

    Using .sortable("toArray") or .serialize() are also options.

    Nice! On to the final bit.

    Serializing & sending off the new sorted order (Step 3)

    Serialization is “the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link)” (thanks Wikipedia!)

    How you do this depends a lot on your specific needs, so we’ll just discuss some of the ways you could get it done using jQuery.

    AJAX:

    If we use AJAX, we can just shoot off a request to the server with the new order. jQuery will automatically handle serializing listValues for us:

    $.post("your-server.com/save_order", { "items": listValues } );
    

    Or if you prefer JSON:

    $.post("your-server.com/save_order", JSON.encode({ "items": listValues }) );
    

    Form

    Create a form:

    <form action="your-server.com/save_order" method="POST">
        <input name="items" value="" />
    </form>
    

    Update the item input:

    var serializedValue = $.param(listValues);
    
    $("#ourForm > input").val(JSON.encode(listValues));
    

    Send it:

    $("#ourForm").submit()
    

    Old answer:

    HTML:

    <form action="save_order.php" method="POST" style="display: none;">
    <input name="new_order" value="" type="hidden" />
    </form>
    

    JavaScript:

    $(".selector").sortable({
        stop: function(event, ui) {
            var data = "";
    
            $("#sortable li").each(function(i, el){
                var p = $(el).text().toLowerCase().replace(" ", "_");
                data += p+"="+$(el).index()+",";
            });
    
            $("form > [name='new_order']").val(data.slice(0, -1));
            $("form").submit();
        }
    });
    

    And in save_order.php, you can parse the POST variable “new_order” and get the orders of Item 1, Item 2, Item 3, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a website where there is a lengthy list of items to display
Let's say I have a URL: http://example.com/person/list This website will display a list of
I have a large list of users that registered through a website without any
I have an ASP.Net hosted website which displays a list of results as a
i have a list of tags on a website next to each link and
I would like to have a list of browsers which don't support a website
Okay, so here's my problem: I have a list of members on a website,
i have a website who have a list of information on the front page.
Here's what I'm trying to do. I have an extremely large list of items.
If I have List<String> text how can I create a sub-list of all continious

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.