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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:15:07+00:00 2026-06-18T01:15:07+00:00

OK, so I grabbed this code snippet from another questions and cant for the

  • 0

OK, so I grabbed this code snippet from another questions and cant for the life of me remember which one, otherwise I would just try to resurrect that question. This is what I am trying to accomplish:

I have various divs and want to me able to drag them and have them swap positions, so if I grab the red square and drop in on the blue square, they will swap places (snap into their positions). I am just trying to get the really basic code working now, and once I get that I will implement differing style sheets when they are swapped to change the content and look.

The problem I have with the current code is when I drag and drop it will drop back to its original position and create a duplicate div after my other divs. Hope this makes sense, here is the jquery:

$(document).ready(function () {
    src = null;
    options = {
        revert:true,
        /*axis: 'x',*/
        opacity: 0.8,
        start: function() {
            src = $(this).parent();
        }
    }

    $(".item").draggable(options);
    $(".container").droppable({
        drop: function(event, ui) {
            src.append(
                $('.item', this).remove().clone()
                    .removeClass().addClass("item")
                    .css({"left": '', "opacity": '',"top":''})
                    .draggable(options)
                );

            $(this).append(
                ui.draggable.remove().clone()
                    .removeClass().addClass("item")
                    .css({"left": '', "opacity": '',"top":''})
                    .draggable(options)
                );
        }
    });

});

return this.pushStack( stack );

The markup is this:

<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>

Styling should matter here, but if it does, I just set them to 100x100px, one red, one blue, and one green.

  • 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-18T01:15:08+00:00Added an answer on June 18, 2026 at 1:15 am

    Here is a solution to this problem, withou using of swap plugins, just jQuery UI.
    Fiddle: http://jsfiddle.net/promatik/YtQRd/

    jQuery UI:

    $(".item").on("mouseup", function(){
       $(this).css("top", "").css("left", "");
    });
    
    $(".item").draggable().droppable({
        drop: function( event, ui ) {
            draged = ui.draggable.css("top", "initial").css("left", "initial");
            dropped = $(this);
            var elems = draged.parent().children();
            if( elems.index(draged) > elems.index(dropped) ) {
                temp = draged;
                draged = dropped;
                dropped = temp;
            }
            var nElem = draged.next();
            dropped.after(draged);
            nElem.before(dropped);
        }
    });
    

    This will do the swap of the objects dragged and dropped.

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

Sidebar

Related Questions

I grabbed this code form JCarousel and just trying to understand these lines below.
My project is a large one, and I'm not sure which code snippet is
I grabbed this compact news reader from here Below is the code for animating
So I have this ruby code I grabbed from wikipedia and I modified a
I grabbed this code form some book I've bumped on the InternetS... sm: new
I have written this code (this is a snippet) that doesn't seem to be
I know there are a few similar questions to this but I just can't
I've grabbed some Scala CSV parsing code from here: Use Scala parser combinator to
I grabbed this piece of code: ClassABC abc = new ClassABC(); for (Field field
I'm trying to create Highcharts chart from mysql, I have code like this: xAxis:

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.