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 8360819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:33:24+00:00 2026-06-09T11:33:24+00:00

I have a sortable list (not jQuery UI sortable, just by a simple click

  • 0

I have a sortable list (not jQuery UI sortable, just by a simple click on buttons).
Sorting to the right via after() works just fine, but sorting to the left with before() does not work.

First of all the code:

Html

PhotoList:

<ul class="PhotoList">
   <li>
       <div class="ThumbWrapper">
           <img src="images/img_testphoto.jpg" />
       </div>
   </li>
   [...more Elements...]
</ul>

Selectable List:

(positioned absolutely over the photos)

<ul class="SelectablePolaroids">
  <li>
    <div class="Selectable">
        <div class="Tools">
            <div class="ArrowLeft Tool">
            </div>
            <div class="Delete Tool">
            </div>
            <div class="ArrowRight Tool">
            </div>
        </div>
    </div>
  </li>
  [...more Elements...]
</ul>

JS

Bind functions

jQuery('.SelectablePolaroids .Selectable .ArrowLeft').live('click', function(){sortPolaroid(jQuery(this),0)});
jQuery('.SelectablePolaroids .Selectable .ArrowRight').live('click', function(){sortPolaroid(jQuery(this),1)});

Function

function sortPolaroid(elm, right)
{
    var selectitem = elm.parents('li');
    var index = jQuery('.SelectablePolaroids').children().index(selectitem);
    var photo = jQuery('.PhotoList').children().eq(index);
    selectitem.remove();
    photo.remove();
    if(right)
    {
        jQuery('.SelectablePolaroids').children().eq(index).after(selectitem);
        jQuery('.PhotoList').children().eq(index).after(photo);
    }
    else
    {
        console.log(index);
        jQuery('.SelectablePolaroids').children().eq(index).before(selectitem);
        jQuery('.PhotoList').children().eq(index).before(photo);
    }
 }

If right is true or .ArrowRight was clicked, it works just as expected. The item is removed and inserted one position further to the right.
As you can see I logged index to check if the else statement is executed at all and whether the index is correct. And yeah the else statement is executed and the index is correct as well. Is before() working in another way than after() or am I just blind to another mistake?

EDIT:

I have also logged the index after before().

//...
else {
   console.log(index);
   jQuery('.SelectablePolaroids').children().eq(index).before(selectitem);
   console.log(jQuery('.SelectablePolaroids').children().index(selectitem));
   jQuery('.PhotoList').children().eq(index).before(photo);
}

And it stays the same, nothing changes at all… but it was remove, which means it is inserted at exactly the same position, therfore it works if I am doing the following – my Solution:

//...
else {
    jQuery('.SelectablePolaroids').children().eq(index-1).before(selectitem);
    jQuery('.PhotoList').children().eq(index-1).before(photo);
}

But I don’t really know why… the new element should be at the index, and before should insert it before index…
If someone knows please answer I will gladly accept an answer 🙂

…

Okay, figured it out by myself and answered, sorry about the ruckus, but maybe some other people don’t know as well and are thinking in the same way as me 🙂

  • 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-06-09T11:33:25+00:00Added an answer on June 9, 2026 at 11:33 am

    By removing the element the next and not the previous element gets the index, so it will be inserted into the same position.
    It works with after() because the element getting the new index is in the “right direction”, therefore it works with the following:

    //...
    else
    {
        jQuery('.SelectablePolaroids').children().eq(index-1).before(selectitem);
        jQuery('.PhotoList').children().eq(index-1).before(photo);
    }
    

    Just a logical mistake… solved after appropiate thinking 🙂

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

Sidebar

Related Questions

I have two lists. They are sortable but not connected (elements of list one
Basically I have a draggable list which is connected with a sortable list. Works
Little help need with jquery sortable. I have a nested list like so: <div
I have a sortable list using JQueryUI and use the JQuery Each function to
I am looking for a method to have a JQuery-like sortable list ( link
I am having problems with what should be a simple jQuery-UI droppable/sortable list. In
We have a sortable list using JQuery UI Sortable that we are trying to
I have a list that uses Jquery UI drag drop sortable but when I
I have a sortable list like this one: http://jqueryui.com/demos/sortable Is it possible to get
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis

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.