473,486 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Identify last selection made on list box

Claus Mygind
571 Contributor
I have a list box and want to limit the user to selecting a max of 5 items from the list. I've put in a counter which warns the user that more than 5 items have been selected, however I cannot reverse the users selection of the last item in the select box as I cannot identify which of the items was the last. I store the list of selected items in a hidden field on change. That one is no problem, but I want to reflect which items have been selected.

The options shown are just examples, the actual list is much longer. It would be nice if I could identify the current selection. Right now I only see one option and that is to repopulate the options array making selected items by what is found in the hidden field.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <SELECT 
  3.      NAME="rDEPT"
  4.      id  ="rDEPT"
  5.      multiple
  6.      class="InputText"
  7.      onChange="assembleDept(this);"
  8.  >
  9.     <option value="-1" SELECTED>Select Pay Type</option>
  10.     <option value="A">Annual</option>
  11.       <option value="B">Bi-Weekly</option>
  12.        <option value="C">Contract</option>
  13.      <option value="H">Hourly</option>
  14.     <option value="P">Part-Time</option>
  15.       <option value="S">Salaried</option>
  16.  
  17. </select>
  18.  
  19.  
  20. function assembleDept(obj)
  21. {
  22. //need to alert if more than 5 items selected
  23.     var cNewVal = ""
  24.     var cCount = 0
  25.     for(i=0;i<document.getElementById("rDEPT").options.length;i++)
  26.     {
  27.         if (document.getElementById("rDEPT").options[i].selected )
  28.         {
  29.             cCount += 1;
  30.             if (cCount > 5)
  31.             {
  32.                 alert("you have selected more than 5 departments");
  33.             }else{
  34.                 cNewVal +=              document.getElementById("rDEPT").options[i].value.substring(0,6)+"~";
  35.             }
  36.         }
  37.     }
  38.  
  39.     document.getElementById("REVIEWDEPT").value = cNewVal
  40. }
  41.  
  42.  
Jan 16 '09 #1
5 2964
Dormilich
8,658 Recognized Expert Moderator Expert
@Claus Mygind
but you write the actual value to cNewVal, thus cNewVal resembles the selection order (you could write the values to an array to have better access though)
Jan 16 '09 #2
Claus Mygind
571 Contributor
@Dormilich
Thank you for your quick response.

While you were posting your reply that is exactly what I did (see my code below)

However my question is still, "Can you detect which option the user selected or must you always cycle through the options array?".

Note the array rDepartments is created and loaded at the time the page is served.
Expand|Select|Wrap|Line Numbers
  1. function assembleDept(obj)
  2. {
  3. //need to alert if more than 5 items selected
  4.     var cNewVal = ""
  5.     var cCount = 0
  6.     for(i=0;i<document.getElementById("rDEPT").options.length;i++)
  7.     {
  8.         if (document.getElementById("rDEPT").options[i].selected )
  9.         {
  10.             cCount += 1;
  11.             if (cCount > 5)
  12.             {
  13.                 alert("you have selected more than 5 departments");
  14.                 rePopulate();
  15.             }else{
  16.                 cNewVal += document.getElementById("rDEPT").options[i].value.substring(0,6)+"~";
  17.             }
  18.         }
  19.     }
  20.  
  21.     document.getElementById("REVIEWDEPT").value = cNewVal
  22. }
  23. function rePopulate()
  24. {
  25.     document.getElementById("rDEPT").options.length = 0
  26.     for (var i = 0; i < rDepartments.length; i++ )
  27.     {
  28.         //get the stored array value        
  29.         var cThisValue = rDepartments[i].substring(0,6)
  30.         //test if this value is in the hidden field REVIEWDEPT
  31.         var cIsSelected = ( document.getElementById("REVIEWDEPT").value.indexOf(cThisValue)!=-1 ) ? true : false ;
  32.         //if the value is S for salaried the display text must be properly displayed
  33.         if (cThisValue == "S")
  34.         {
  35.             document.getElementById("rDEPT").options[i] = new Option("Salaried", rDepartments[i], cIsSelected, cIsSelected);
  36.         }else{
  37.             document.getElementById("rDEPT").options[i] = new Option(rDepartments[i], rDepartments[i], cIsSelected, cIsSelected);
  38.         }
  39.     }
  40. }
  41.  
Jan 16 '09 #3
Dormilich
8,658 Recognized Expert Moderator Expert
@Claus Mygind
right now you have to cycle to the options array unless you give every option an id and store that in the array (you might even write a class that does all the difficult stuff).
Jan 16 '09 #4
Dormilich
8,658 Recognized Expert Moderator Expert
just out of interest, does the field "REVIEWDEPT" is used for anything else than the immediate storage? is it used after form submission? if not, you can replace it with an object.
Jan 17 '09 #5
Claus Mygind
571 Contributor
Yes ReviewDept is a data field in my table.
Jan 22 '09 #6

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

Similar topics

1
1446
by: | last post by:
Hi With extended multi-select, is it possible to know the last selection made by the user? Thanks Regards
3
6592
by: Melissa | last post by:
I have this table: TblProjectYear ProjectYearID ProjectYearStartDate ProjectYearEndDate The Project Year will always span across December 31; for example 9/1/04 to 6/30/05. How do I build a...
8
2538
by: Galina | last post by:
Hello I have 6 dependent list boxes on my ASP page:  Faculty;  Lecturer;  Course;  Course occurrence;  Group;  Week commencing date. When faculty is selected, lists of lecturers and...
2
1519
by: slitchfield | last post by:
Sorry if this is real beginner's stuff, but I'm an old-school HTML guy and this is all another world to me. Is there an easy way to get the user to specify a number from 1 to 5 (in a pick list...
5
4551
by: srampally | last post by:
I need the capabilty to hide/show a selection list, just the way its done at http://www.lufthansa.com (place the cursor over "Group Companies"). However, I am looking for a javascript that is much...
10
3385
by: mukeshrasm | last post by:
Hi I have a html in which there is two selection box and two radio button. Radio buttion is of array type. What I want if user will click the radio button then only he/she be able to make...
7
14454
by: robtyketto | last post by:
Greetings, I'm slowly building up code to do the following:- Display TWO selection option boxes (cascading). If the FIRST selection option box changes then reload the jsp using onchange...
8
3886
by: bluemoon9 | last post by:
Does anyone know how to write a code to promt the selection at the last record in the list box?. I have this code below in the "On Click" event, when user click on the code, it close a form, requery...
6
5164
by: David Wright | last post by:
Hello Folks I am using Microsoft Access 2000 I would be grateful if someone could help me with “Dlookup”. I tried various methods of writing Dlookup and various events to trigger it, none of...
0
7094
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
6964
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...
0
7123
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,...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5427
agi2029
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,...
0
3066
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...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.