473,851 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript for Adding & Removing Items from a Listbox

17 New Member
Title: Javascript for Adding & Removing Items from a one Listbox to another
Author: Vivek Kapile
Email: snipped
Language: JavaScript
Platform: Javascript in ASP.net
Technology: Used in ASP.net
Level: Beginner

This code will help for the begineers, who want to learn, how to move the the items from one list box to another listbox with up and down facility.
On this topic I have covered Adding items from one listbox to another listbox with moving the items from Up and Down within a listbox through javascript, hope this will help for the begineers.

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  3. //Creating a global variable 
  4.     var availableList;
  5.     var selectedList;
  6. //function that used to ADD a items from one list box to another listbox
  8.     function AddListItem() {
  9.         availableList = document.getElementById("lstAvailable");
  10.         selectedList = document.getElementById("lstSelected");
  11.         if (availableList.selectedIndex < 0) {
  12.             return;
  13.         }
  14.         else {
  15.             selectedList.appendChild(availableList.options.item(availableList.selectedIndex));
  16.             selectNone(selectedList, availableList);
  17.         }
  18.     }
  20. //function that used to REMOVE a items from one list box to another listbox
  21.     function RemoveListItem() {
  22.         if (selectedList.selectedIndex < 0) {
  23.             return;
  24.         }
  25.         else {
  26.             availableList.appendChild(selectedList.options.item(selectedList.selectedIndex));
  27.             selectNone(selectedList, availableList);
  28.         }
  29.     }
  30. //function that used to Move Up the items from listbox
  31.     function MoveUpItems() {
  32.         availableList = document.getElementById("lstAvailable");
  33.         var slectedItemIndex = availableList.selectedIndex;
  34.         var selectedItemText = availableList[slectedItemIndex].text;
  35.         var selectedItemValue = availableList[slectedItemIndex].value;
  36.         var selectedItemTextToMove = availableList[slectedItemIndex - 1].text;
  37.         var selectedItemValueToMove = availableList[slectedItemIndex - 1].value;
  38.         availableList[slectedItemIndex].text = selectedItemTextToMove;
  39.         availableList[slectedItemIndex].value = selectedItemValueToMove;
  40.         availableList[slectedItemIndex - 1].text = selectedItemText;
  41.         availableList[slectedItemIndex - 1].value = selectedItemValue;
  42.         availableList = slectedItemIndex - 1;
  43.     }
  44. //function that used to down the items from listbox
  45.     function MoveDownItems() {
  46.         availableList = document.getElementById("lstAvailable");
  47.         var selectedItemIndex = availableList.selectedIndex;
  48.         var selectedItemText = availableList[selectedItemIndex].text;
  49.         var selectedItemValue = availableList[selectedItemIndex].value;
  50.         var moveText = availableList[selectedItemIndex + 1].text;
  51.         var moveValue = availableList[selectedItemIndex + 1].value;
  52.         availableList[selectedItemIndex + 1].text = selectedItemText;
  53.         availableList[selectedItemIndex + 1].value = selectedItemValue;
  54.         availableList[selectedItemIndex].text = moveText;
  55.         availableList[selectedItemIndex].value = moveValue;
  56.         availableList = selectedItemIndex + 1;
  57.     }
  58. //Function is used to remove a selection
  59.     function selectNone(list1, list2) {
  60.         list1.selectedIndex = -1;
  61.         list2.selectedIndex = -1;
  62.         addIndex = -1;
  63.         selIndex = -1;
  64.     }
  65.  </script>

There are many topics to learn with Javascript. I have covered a small portion hope this will helps all the beginners to startup. Please give your feedback and suggestion.


* Written on 16-March-2011, Wednesday




About the Author

Vivek kapile
Mar 16 '11 #1
0 7739

Sign in to post your reply or Sign up for a free account.

Similar topics

by: John R. | last post by:
I want to have a listbox that shows a checkbox and a textbox. I created a user control that has a checkbox and a textbox in it and have been trying to add it to a listbox but I can't get it to show up for each item I add to the listbox. foreach (Column column in columns) { ColumnsListBox.Items.Add(column.Name); ColumnsListBox.Controls.Add(new ColumnListControl(true, column.Name,
by: Robin Tucker | last post by:
SelectedItems and SelectedIndices appear to be read-only, so I cannot remove a selected item. I need to do this to synchronise the selection in my list with the selection in other views (ie. select in another view and the selected items in my list change). How can I change the selected items in a list box!??
by: vb | last post by:
Hi, I am new to .Net. I am using a Combo Box in my windows forms. I am adding the items by creating the instances and adding the same to the list. My questions/doubts are: 1. If I have 25 to 30 options in each combo box and like that if we have 6 or 7 combo boxes in a form, isnt it a problem in creating that number of instances everytime the form is opened/activated 2. How do I dispose the Combo Box items.
by: Husam | last post by:
Hi EveryBody: I made project by Vb.Net which consist the following items: 1\ Textbox 2\Button 3\Listbox When you write any thing in the textbox and press the button any text written in the textbox will be added to the listbox, but i figure that when
by: John Dalberg | last post by:
I would like to select items from a listbox into another listbox using drag and drop functionality. Does ASP.NET 2.0 webparts support this? Or any open source free framework? I am not looking for anything Atlas based. John dalberg
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not pattern1 print >>orcfilename, line I am pretty sure my code isn't close to what I want. I need to be able
by: mharness | last post by:
Hello, Under most circumstances I don't have any trouble removing items from a dropdownlist but this time it's not working. I have a ddl that list items which, when choosen are added to a gridview on the same page and removed from the ddl so that they can only be added once. What I want to do and can't seem to make work is the code that removes the ddl items that are already on the gridview at page load.
by: chathura86 | last post by:
hi, i want to remove selected items from a list box this is the code i'v used Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click, lstSelNum.DoubleClick For Each i As Integer In lstSelNum.SelectedIndices txtSearch.Text += i & " - " lstSelNum.Items.RemoveAt(i)
by: questionit | last post by:
Hi How to delete/ remove all items in a "Value List" ListBox with a button click? I have tried this: For i = 0 To Me.List1.ListCount - 1 Me.List1.RemoveItem (i)
by: LayneMitch via WebmasterKB.com | last post by:
I'm trying to add more form options depending on what you chose and then remove the form options if the main option is deselected. The latter is what I'm having the problem with. I only need suggestions on the checkbox options, not the radio options. Depending on what advice I get for the checkbox options, I can alter the code for the radio options. The form code is very long so I'll only post the problem area and it's related...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.