18

How can I move items from one list box control to another listbox control using JavaScript in ASP.NET?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
balaweblog
  • 14,982
  • 28
  • 73
  • 95
  • I am removing the asp.net tag, as this really doesn't have anything to do with asp.net. – James McMahon Apr 13 '09 at 17:38
  • 1
    @balaweblog: Are you trying to use the ASP.NET ListBox control as your base that you want to move items into/out of? If so, the ASP.NET tag is valid and I'll re-add it and post a solution that will help on the ASP.NET side of things. – Chris Porter Sep 21 '09 at 18:31

3 Answers3

32

If you're happy to use jQuery, it's very, very simple.

$('#firstSelect option:selected').appendTo('#secondSelect');

Where #firstSelect is the ID of the select box.

I've included a working example here:

http://jsbin.com/aluzu (to edit: http://jsbin.com/aluzu/edit)

nickf
  • 537,072
  • 198
  • 649
  • 721
Remy Sharp
  • 4,520
  • 3
  • 23
  • 40
  • I am using javascript could you please provide me in javascript – balaweblog Oct 15 '08 at 09:37
  • 1
    You shouldn't use remove() here. It will delete any event handlers you might have. $("#firstSelect option:selected").appendTo("#secondSelect") will retain any events, and has the exact same effect. – nickf Oct 15 '08 at 13:50
  • 1
    @balaweblog: That is javascript – user9991 Jan 02 '09 at 19:26
6

This code assumes that you have an anchor or that will trigger to movement when it is clicked:

document.getElementById('moveTrigger').onclick = function() {

    var listTwo = document.getElementById('secondList');
    var options = document.getElementById('firstList').getElementsByTagName('option');

    while(options.length != 0) {
        listTwo.appendChild(options[0]);
    }

 }
Tom
  • 15,527
  • 5
  • 48
  • 62
  • I don't think this would remove the option from the list you are moving it from. – James McMahon Apr 13 '09 at 17:37
  • @nemo Try it and see. It works fine. The appendChild() function removes the option from it's current parent and moves it to that which is specified; otherwise, you'd be duplicating the node in the DOM. – Tom Apr 14 '09 at 13:01
  • 2
    @nemo Not a problem! In my opinion, it's always nice to know *how* something works instead of just *that* it works. – Tom Apr 14 '09 at 23:23
6

A library-independent solution:

function Move(inputControl)
{
  var left = document.getElementById("Left");
  var right = document.getElementById("Right");
  var from, to;
  var bAll = false;
  switch (inputControl.value)
  {
  case '<<':
    bAll = true;
    // Fall through
  case '<':
    from = right; to = left;
    break;
  case '>>':
    bAll = true;
    // Fall through
  case '>':
    from = left; to = right;
    break;
  default:
    alert("Check your HTML!");
  }
  for (var i = from.length - 1; i >= 0; i--)
  {
    var o = from.options[i];
    if (bAll || o.selected)
    {
      from.remove(i);
      try
      {
        to.add(o, null);  // Standard method, fails in IE (6&7 at least)
      }
      catch (e)
      {
        to.add(o); // IE only
      }
    }
  }
}

HTML

<select id="Left" multiple="multiple" size="10">
  <option>Some</option>
  <option>List</option>
  <option>Of</option>
  <option>Items</option>
  <option>To</option>
  <option>Move</option>
  <option>Around</option>
</select>

<div id="Toolbar">
  <input type="button" value="&gt;" onclick="Move(this)"/>
  <input type="button" value="&gt;&gt;" onclick="Move(this)"/>
  <input type="button" value="&lt;&lt;" onclick="Move(this)"/>
  <input type="button" value="&lt;" onclick="Move(this)"/>
</div>

<select id="Right" multiple="multiple" size="10">
</select>

CSS (example)

select { width: 200px; float: left; }
#Toolbar { width: 50px; float: left; text-align: center; padding-top: 30px; }
#Toolbar input { width: 40px; }

Quick test FF3 and IE6 & 7 only.

PhiLho
  • 40,535
  • 6
  • 96
  • 134
  • This solution didn't work for me in ASP.NET because I needed the controls to handle their own ViewState (previously selected values) between posts but it did work really well for simple HTML/Javascript manipulation of the select lists. – Chris Porter Sep 21 '09 at 19:59