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

  • Home
  • SEARCH
  • 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 6110569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:31:51+00:00 2026-05-23T14:31:51+00:00

Hod do you determine the new index of an element sorted using jQuery UI

  • 0

Hod do you determine the new index of an element sorted using jQuery UI sortable?

i have an un-ordered list and every list item has an id…when i drag it up or down and place it, i need to know the new position (vaguely speaking, like the id of element that moved to make room for this new element) …is that even possible?
i tried using the .mousedown and .mouseup events and getting this.id…but it returns the same element being dragged both the times.

In short we knw the element being dragged but hw can we know where it was finally laid to rest?

the code is simple…it is actually a table

---------HTML----------------
<table id="activities">
<thead>
<tr><th>Activity Name</th><th>Activity Number</th></tr>
</thead>
<tbody>
<tr id="node-1">
<td>Welcome</td>
<td>1</td>
</tr>
<tr id="node-2">
<td>Eat</td>
<td>2</td>
</tr>
<tr id="node-3">
<td>Walk</td>
<td>3</td>
</tr>
<tr id="node-4">
<td>Sleep</td>
<td>4</td>
</tr>
</tbody>
</table>

Now im making the rows sortable by jquery:

$("#activities tbody").sortable();

Now when the user drags a row up or down, i need to change the activity number accordingly…for example if he drags the “welcome” row down under the “eat” row, that will mean the the activity number of “welcome” row is now 2 (which was previously 1) and the activity number of “eat” row is 1….to display the new updated table with correct activity number, i need to know the where it was dropped.

  • 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-23T14:31:52+00:00Added an answer on May 23, 2026 at 2:31 pm

    You can use the beforeStop function within the sortable() method:

    $("#activities tbody").sortable(
        {
            beforeStop: function(event, ui) {
                newIndex = $(ui.helper).index('table tbody tr');
                alert(newIndex);
            }
        }
    );
    

    JS Fiddle demo;


    Edited to provide information on retrieving the id of the dropped element:

    $("#activities tbody").sortable(
        {
            beforeStop: function(event, ui) {
                var index = $(ui.helper).index( 'table tbody tr' );
                alert( 'index = ' + index + '; id = ' + $(ui.helper).attr( 'id' ) );
            }
        }
    );
    

    JS Fiddle demo.


    Edited in response to question, in comments, from the OP:

    …problem solved!! If it’s not a matter of trouble for you, can you explain the code..?

    Of course:

    • newIndex = $(ui.helper).index('table tbody tr');
      This selects the current ‘helper element’, and uses the jQuery index() method to find where that element fits within the elements returned by the selector passed to the method, in this case the elements returned by table tbody tr. If there is more than one table and tbody on the page this index will be wrong, so selecting by the table’s id would be (index('#idOfTable tbody tr')) would be preferable.
    • theID = ui.helper.id;
      This returns the id of the item held in the ui.helper variable (the just-dropped element).
    • The alert() is just a routine JavaScript alert.

    References:

    • sortable()
    • beforeStop (from the above link, select the ‘events’ menu, and then click on beforeStop):

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

    Code examples

    Supply a callback function to handle the beforeStop event as an init option.

            $( ".selector" ).sortable({
                beforeStop: function(event, ui) { ... }
            });
    
    • index().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have added new event i.e Gridview_Onclick event dynamically in RowDatabound event. But when
I have a dictionary that looks like the below. ex1_pattern = {'ex':0,'country':1,'dow':2,'hod':3,'adx':4,'vid1':5} I would
Hod Do i hide the parent div EventsRollup if the child div RelatedEventsList is
I have a string and I want to decode into json. The string was
Have two table in mysql database with table1 and table2, table1 id marks hodname
I have this code, which selects content types from database, and then build its
I have few textboxes on one page and one user control. What I want
Protected Sub lnkContractors_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkContractors.Click If Context.User.IsInRole("HOD")
can any one help me regarding the events related to menubar sub items, here
Ok this question is not exactly technical but very pertinent and current. If you

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.