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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:46:31+00:00 2026-06-15T19:46:31+00:00

i have seen a question already asked and answered about swapping the values/items between

  • 0

i have seen a question already asked and answered about swapping the values/items between two drop-down lists, How to swap values in select lists with jquery?

My question is can it be done between to jquery ui-selectable lists. where you click one element in list 1 and another in list 2 and clicking a swap button would transfer the contents of each item/div that was selected from one to the other?

HTML:

<button>Swap Values</button>
<div style="display:inline-block">
<div id="selectable" class="mySel">
    <div value=Item1>Item1</div>
    <div value=Item2>Item2</div>
    <div value=Item3>Item3</div>
    <div value=Item4>Item4</div>
</div>

<div id="selectable2" class="mySel2">
    <div value=Item1>Item1</div>
    <div value=Item2>Item2</div>
    <div value=Item3>Item3</div>
    <div value=Item4>Item4</div>
    <div value=Item1>Item5</div>
    <div value=Item2>Item6</div>
    <div value=Item3>Item7</div>
    <div value=Item4>Item8</div>
</div>
</div>

CSS:

*{padding:6px; font-size:1.1em}

.mySel, .mySel2{
height: 300px;
width: 200px;
overflow: auto;
border: solid 2px black;
margin-bottom: 20px;
float:left;
margin:10px;
}

.mySel > DIV, .mySel2 > DIV{
height: 50px;
border: solid 1px red;
}

#selectable .ui-selecting { background: #FECA40; }
#selectable .ui-selected { background: #F39814; color: white; }
#selectable2 .ui-selecting { background: #FECA40; }
#selectable2 .ui-selected { background: #F39814; color: white; }

JQUERY selectable:

$("#selectable, #selectable2").selectable();

JQUERY sortable:

$(".mySel, .mySel2").sortable({
    tolerance: 'pointer',
    connectWith: '.mySel, .mySel2',
    helper: 'original',
    scroll: true
});

JQUERY Swap values attempted code:

$('button').click(function() {
    var new1 = [];

    $('.mySel, .mySel2').each(function(i) {
        new1[i] = $(this).val();
    });
    new1.reverse();
    $('.mySel, .mySel2').each(function(i) {
        $(this).val(new1[i]);
    });
});
  • 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-15T19:46:33+00:00Added an answer on June 15, 2026 at 7:46 pm

    This will work without the sortable() method, it seems to interfere with the selectable() method.

    $('button').click(function(){
       $('#selectable .ui-selected').removeClass('ui-selected').appendTo('#selectable2');
       $('#selectable2 .ui-selected').removeClass('ui-selected').appendTo('#selectable');
    });
    

    edit: It will work with the sortable method, as long as sortable() runs second.
    *edit: in response to followup questions*

    Additional Questions

    How to tell if an element with a value of “Item1” already exists in list2 before moving it over?

    There were a few fun bit to work out here. The first is whether or not the identical term in the opposite list was being swapped or not. If it is, the swapping can continue because both lists will continue to only have one of each value. Second, if a duplicate is found, the script must not complete the swapping. Both sets of lists needed to be checked for duplicates before the swap could occur.

    I fixed this with a combination of some complicated jQuery selectors and a separate evaluation function that would either return true or false, depending on whether or not there were duplicates. I’ve added comments to the code below explaining how it works.

    function testFor($o, $p) { //two collections of Items we are comparing
            var retVal = true; //By default, this script returns true, a mismatched pair results in a return: false;
            $o.each(function() { //Look at each element in the first collection
                var thisDivValue = $(this).attr('value'); //Grab it's value=""
                if ($p.parent().find('div[value=' + thisDivValue + ']').not('.ui-selected').length > 0) { 
                //Search in the opposite collection for an item with a matching value that is NOT selected for swapping, if you find it, return false.
                    retVal = false;
                    return false;
                }
                else {return true;}
            });
            return retVal; //True or false depending on what was set above.
        }
    function showError() {
        alert('DUPLICATE FOUND');
    }
    $("#selectable, #selectable2").selectable();
    $(".mySel, .mySel2").sortable({
        tolerance: 'pointer',
        connectWith: '.mySel, .mySel2',
        helper: 'original',
        scroll: true
    });
    $('button').click(function() {
        var v = $('#selectable2>div');
        var w = $('#selectable>div');
        var x = $('#selectable .ui-selected');
        var y = $('#selectable2 .ui-selected');
        if ((w.length - x.length + y.length) > 4) {return false;}
        if ((v.length - y.length + x.length) > 8) {return false;}
        if (testFor(x, y) && testFor(y, x)) { //Both of these tests must return TRUE in order for the swap to happen
            $('#selectable .ui-selected').removeClass('ui-selected').appendTo('#selectable2');
            $('#selectable2 .ui-selected').removeClass('ui-selected').appendTo('#selectable');
        }
        else {showError();}
    });​
    

    jsFiddle

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

Sidebar

Related Questions

I have seen this question already answered here but when I tried the same
I have seen the question: Communication between two separate Java desktop applications (answer: JGroups)
I have already seen this question Draw a line with two mouse clicks on
I have seen the related question here: An object with the same key already
I have seen this question that talks about getting the last part of a
I have seen this question about deploying to WebSphere using the WAS ant tasks.
I have seen this question asked in a couple of different ways on SO
I have question about sending emails from MVC3 application. I already read for exmple
I know this question has been asked before and I have seen a plethora
I have seen this question already but it does not explain it so that

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.