473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Add/remove options from list box based on selection in another list box

Hai.i am new to javascript coding.i have two listboxes.when the user selects items in first listbox .it has to be removed from second listbox.if he unselects it has to be added again.please help me
Sep 16 '08 #1
9 9730
Atli
5,058 Expert 4TB
Hi.

You could create the two boxes and populate the first one with the options you want.
Then you could create a function that clones each unselected option from the first box into the second box, and have the first box's onchange event trigger the function.

This may come in handy when writing that function:
Expand|Select|Wrap|Line Numbers
  1. // Get a <select> box with the ID 'mySelect'
  2. elem = document.getElementById('mySelect');
  3.  
  4. // Get a list of options in that select box
  5. options = elem.options;
  6.  
  7. // See the amount of options in the list
  8. alert(options.length); 
  9.  
  10. // See whether the fist option in the list is selected
  11. alert(options.[0].selected) 
  12.  
  13. // Clone the first option and add it to another <select> box
  14. var clone = options[0].cloneNode(true);
  15. document.getElementById('second').appendChild(clone);
  16.  
Sep 16 '08 #2
Thank u .Code is very helpful to me.

i have one more constrainst .we have to do vice versa.

User can select from second list box that has to be removed from first list box.

before adding to the second list box i have to check whether that list item already exist or not.
Sep 16 '08 #3
I have two listboxes.when the user selects items in first listbox .it has to be removed from second listbox.if he unselects it has to be added again.

i have one more constrainst .we have to do vice versa.

User can select from second list box that has to be removed from first list box.

before adding to the second list box i have to check whether that list item already exist or not.

Thank u for reply
Sep 17 '08 #4
stepterr
157 100+
I have two listboxes.when the user selects items in first listbox .it has to be removed from second listbox.if he unselects it has to be added again.

i have one more constrainst .we have to do vice versa.

User can select from second list box that has to be removed from first list box.

before adding to the second list box i have to check whether that list item already exist or not.

Thank u for reply
ramani253,
Do you have any of this coded yet? If so what is happening? How are you filling the list boxes. For example, are they hard coded values or are you filling them from a database?
Sep 17 '08 #5
Iam filling from database
Binding the listbox to datatable which is server side.


intially the two listboxes(include and exlude ) will have same items.

after the selects the items it has to be removed from other list box.



how we write in javascript code to check if that item exists or not.

before adding to second listbox .
Sep 17 '08 #6
Expand|Select|Wrap|Line Numbers
  1. function fchange(member1_ListBox1,member1_listbox2)
  2.     {
  3.             // Get a <select> box with the ID 'mySelect'
  4.         elem = document.getElementById('member1_ListBox1');
  5.  
  6.         listbox2=document.getElementById(member1_listbox2);
  7.  
  8.         // Get a list of options in that select box
  9.         options = elem.options;
  10.  
  11.  
  12.         for (var i=0;i< elem.length;i++)
  13.         {
  14.         if (elem.options[i].selected)
  15.         {
  16.          //remove item from second listbox     
  17.         }
  18.         else
  19.         {
  20.         var clone = options[i].cloneNode(true);
  21.  
  22.         //Here Need to write code whether item exist or not 
  23.         document.getElementById('member1_ListBox2').appendChild(clone); 
  24.         }
  25.         }
  26.  
  27.  
  28.     }
  29.  
  30.  
In code behind
ListBox1.Attributes.Add("onchange", "fchange()");
Sep 17 '08 #7
Is it possible to check value in second list box
Sep 17 '08 #8
acoder
16,027 Expert Mod 8TB
Threads merged. Please do not double post you questions. Also please use code tags when posting code. Thanks!

Moderator.
Sep 17 '08 #9
Atli
5,058 Expert 4TB
You want the second box to have the same items as the first box, except those that are selected, right?

Why not then just clear the second box and then add ever non-selected item to it from the first box?

In other words:
Expand|Select|Wrap|Line Numbers
  1. while( second box has options )
  2. {
  3.   remove the first option
  4. }
  5.  
  6. for( every item in the first box ) 
  7. {
  8.   if( the current item is NOT selected )
  9.   {
  10.     Create a clone of the current item
  11.     Add the clone to the second box
  12.   }
  13. }
  14.  
Sep 18 '08 #10

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

Similar topics

7
by: John C | last post by:
I am working with an HTML-based form that uses a select element that requires about 200 options. Is there a way that I can load, or select one of these options from a file, rather than hardcode...
10
by: JL | last post by:
Does anyone have any ideas on this please? I don't know how to evaluate the no-blank selections against each other from a form as follows: . I have a form with 6 dropdown fields, each...
3
by: Rob | last post by:
Hi, I've got a small javascript problem and I'm kinda stuck. I'm using classic ASP. I have a select box which is populated by a database query and there is a buttom that when clicked it will move...
5
by: Jim Cobban | last post by:
I am trying to create a web page in which the contents of one selection list depends upon which element in another selection list is chosen, but where the information to populate the first...
2
by: kmnotes04 | last post by:
Is it possible to link one drop-down box to another? For example, if a name is chosen from a drop-down list, can another drop-down list then automatically display the person's office as a result of...
0
by: deepak | last post by:
i have set multiple selection property in bith listboxes(html control) to true. i have taken 2 buttons(html control) say Button1,Button2.now i want to add mutiple selected items to another listbox...
7
chunk1978
by: chunk1978 | last post by:
hello. so i have 2 select menus which add and remove options from a 3rd select menu... it seems, however, that it's not possible to use different select menus to toggle a 3rd, because when an...
11
by: Richard Maher | last post by:
Hi, I have read many of the copius entries on the subject of IE performance (or the lack thereof) when populating Select Lists. I don't mind the insert performance so much, (I get 100x120byte...
6
by: Craggy | last post by:
Is it possible to perform a VBA action based on the selection in a list box? I have a table with one column as the "answer" text, and another as the record to go to if this particular answer is...
6
by: troy_lee | last post by:
I have a continuous form that has one combo box (cbo1) with the selectable values of "Removal" and "Installation". I would like to change another combo box (cbo2) value list based on the selection...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...

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.