472,981 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Identify last selection made on list box

Claus Mygind
571 512MB
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
  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>
  17. </select>
  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.     }
  39.     document.getElementById("REVIEWDEPT").value = cNewVal
  40. }
Jan 16 '09 #1
5 2928
8,658 Expert Mod 8TB
@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 512MB
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.     }
  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. }
Jan 16 '09 #3
8,658 Expert Mod 8TB
@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
8,658 Expert Mod 8TB
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 512MB
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

by: | last post by:
Hi With extended multi-select, is it possible to know the last selection made by the user? Thanks Regards
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...
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...
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...
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...
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...
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...
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...
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...
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.