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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:51:22+00:00 2026-05-18T23:51:22+00:00

I have two li elements which are jQuery draggable. When I drag and drop

  • 0

I have two li elements which are jQuery draggable. When I drag and drop box ‘one’ over box ‘two’, they get swapped. So far so good. (The delay fixes another problem described here.) However the elements now are not draggable anymore, even after resetting their draggable option.

Any ideas how to fix this? full working jsfiddle here

<html>
<head>

    <script type="text/javascript" src="includes/jquery-1.4.2.min.js"></script>

    <script type="text/javascript" src="includes/jquery-ui-1.8.2.custom.min.js"></script>

    <script type="text/javascript">

        jQuery.fn.swapWith = function(to) {
            return this.each(function() {
                var copy_to = $(to).clone(true);
                var copy_from = $(this).clone(true);
                $(to).replaceWith(copy_from);
                $(this).replaceWith(copy_to);
            });
        };



        $(document).ready(function() {

            options = { revert: true};

            $("li").draggable(options);

            $('#wrapper').droppable({
                drop: function(event, ui) {
                window.setTimeout("Swap()", 600);
                }
            });
        });

        function Swap() {
            $('#one').swapWith($('#two'));

            //trying to fix problem where elements can't be dragged anymore
            $("li").draggable("destroy"); 
            $("li").draggable(options);
        }
    </script>

</head>
<body>
    <form>
    <ul id="wrapper">
        <li id='one'>
            <div style="width: 100px; height: 100px; border: 1px solid green">
                one<br /></div>
        </li>
        <li id='two'>
            <div style="width: 110px; height: 110px; border: 1px solid red">
                two<br /></div>
        </li>
    </ul>
    <br />
    </form>
</body>
</html>
  • 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-18T23:51:23+00:00Added an answer on May 18, 2026 at 11:51 pm

    So after playing with your code, here’s the conclusion I’ve come to.

    It looks like the draggable jqueryui method is tracking its objects seperately. When you clone, that tracking isn’t cloned. I modified your code as follows:

    jQuery.fn.swapWith = function(to) {
            return this.each(function() {
                var copy_to = $(to).clone(true).appendTo($("#wrapper"));
                var copy_from = $(this).clone(true).appendTo($("#wrapper"));
        //$(to).replaceWith(copy_from);
        //$(this).replaceWith(copy_to);
            });
        };
    

    You can see the fascinating results http://jsfiddle.net/XkUDv/16/

    As you can see, if you drag the new cloned objects, it moves the original, not the cloned one.

    It’s not an answer, but I hope it helps.

    UPDATE:

    after taking a closer look at clone I changed the javascript to:

    <script type="text/javascript">
    
        jQuery.fn.swapWith = function(to) {
            return this.each(function() {
                var copy_to = $(to).clone();
                var copy_from = $(this).clone();
                $(to).replaceWith(copy_from);
                $(this).replaceWith(copy_to);
            });
        };
    
    
    
        $(document).ready(function() {
    
            options = { revert: true };
    
            $("li").draggable(options);
            $('#wrapper').droppable({
                drop: function(event, ui) {
                window.setTimeout(function(){
                    $('#one').swapWith($('#two'));
                    $("li").draggable(options);
                }, 600);
                }
            });
        });
    </script>
    

    Now it works the way you wanted 🙂

    I’m guessing the issue is that since clone(true) copies the event handlers, when you try to reattach the draggable to the new clones it sees the old event handlers and ignores the elements. So instead of clone(true), I changed it to clone();

    hope that helps!

    Working version: http://jsfiddle.net/XkUDv/21/

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

Sidebar

Related Questions

I have two arrays and want to remove from one all elements which exist
I have two elements(dropdowns) which are very closely placed. These elements are required fields.
Ok. So I have two elements on a page which are not located in
I have some page elements which are meant to appear in a two column
I have a somewhat grouping system which all the elements between two divs are
I have two form elements on my page that act as a smooth login
I have a list of elements which I would like to make sortable and
I have following HTML with two elements having the same name <input type=hidden name=
I have a form that has two buttons. One for saving a record and
I have this jQuery plugin called Easy Slider which I'm implementing on this site

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.