In my previous blog, I have explained select and deselect item from listbox and remove selected item from ListBox using JavaScript. Now I am going to explain how to move items across the ListBox such as:

data

After selecting the item from listbox 1 like { Licknow,Goralhpur,Deoria}, click on Move right. After this you can see that the then selected data should be shown in listbox2 like.

Move right

Now select some item from listbox2 like {C++,C#,ANSI C}

listbox2

And now click on Move Left button, data will move to listbox1

Move Left

For this write following code inside body tag.

  1. <form id="form1" runat="server">
  2. <div>
  3. <asp:ListBox ID="lstbox1" runat="server" SelectionMode="Multiple" style="height:200px;width:100px;">
  4. <asp:ListItem Value="1" Text="Delhi" ></asp:ListItem>
  5. <asp:ListItem Value="2" Text="Mumbai" ></asp:ListItem>
  6. <asp:ListItem Value="3" Text="Banglore" ></asp:ListItem>
  7. <asp:ListItem Value="4" Text="Kolkatta" ></asp:ListItem>
  8. <asp:ListItem Value="5" Text="Chennai" ></asp:ListItem>
  9. <asp:ListItem Value="6" Text="Lucknow" ></asp:ListItem>
  10. <asp:ListItem Value="7" Text="Gorakhpur" ></asp:ListItem>
  11. <asp:ListItem Value="8" Text="Deoria" ></asp:ListItem>
  12. </asp:ListBox>
  13. <asp:Button ID="btn1" runat="server" Text="Move right" OnClientClick="moverightleft('lstbox1','lstbox2');return false;" />
  14. <asp:Button ID="btn2" runat="server" Text="Move Left" OnClientClick="moverightleft('lstbox2','lstbox1');return false;" />
  15. <asp:ListBox ID="lstbox2" runat="server" SelectionMode="Multiple" style="height:200px;width:100px;">
  16. <asp:ListItem Value="1" Text="ASP.net" ></asp:ListItem>
  17. <asp:ListItem Value="2" Text="C++" ></asp:ListItem>
  18. <asp:ListItem Value="3" Text="C#" ></asp:ListItem>
  19. <asp:ListItem Value="4" Text="ANSI C" ></asp:ListItem>
  20. <asp:ListItem Value="5" Text="LINQ" ></asp:ListItem>
  21. <asp:ListItem Value="6" Text="SQL" ></asp:ListItem>
  22. <asp:ListItem Value="7" Text="ORacle" ></asp:ListItem>
  23. <asp:ListItem Value="8" Text="D" ></asp:ListItem>
  24. </asp:ListBox>
  25. </div>
  26. </form>

Here a have declared two listbox lstbox1 and lstbox2 and two buttons one for Move Right and one for Move Left.

And write following code for Javascript inside Head tag:

  1. <script >
  2. function moverightleft(list1, list2) {
  3. var lst1 = document.getElementById(list1);
  4. var lst2 = document.getElementById(list2);
  5. for (var i = 0; i < lst1.options.length; i++) {
  6. if (lst1.options[i].selected == true) {
  7. var option = lst1.options[i];
  8. var newOption = document.createElement("option");
  9. newOption.value = option.value;
  10. newOption.text = option.text;
  11. newOption.selected = true;
  12. try
  13. {
  14. lst2.add(newOption, null);
  15. lst1.remove(i, null);
  16. }
  17. catch(error)
  18. {
  19. lst2.add(newOption);
  20. lst1.remove(i);
  21. }
  22. i--;
  23. }
  24. }
  25. }
  26. </script>

Here a function called move right left is used for both purposes like move left and move right.