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. -
-
<SELECT
-
NAME="rDEPT"
-
id ="rDEPT"
-
multiple
-
class="InputText"
-
onChange="assembleDept(this);"
-
>
-
<option value="-1" SELECTED>Select Pay Type</option>
-
<option value="A">Annual</option>
-
<option value="B">Bi-Weekly</option>
-
<option value="C">Contract</option>
-
<option value="H">Hourly</option>
-
<option value="P">Part-Time</option>
-
<option value="S">Salaried</option>
-
-
</select>
-
-
-
function assembleDept(obj)
-
{
-
//need to alert if more than 5 items selected
-
var cNewVal = ""
-
var cCount = 0
-
for(i=0;i<document.getElementById("rDEPT").options.length;i++)
-
{
-
if (document.getElementById("rDEPT").options[i].selected )
-
{
-
cCount += 1;
-
if (cCount > 5)
-
{
-
alert("you have selected more than 5 departments");
-
}else{
-
cNewVal += document.getElementById("rDEPT").options[i].value.substring(0,6)+"~";
-
}
-
}
-
}
-
-
document.getElementById("REVIEWDEPT").value = cNewVal
-
}
-
-
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)
@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. - function assembleDept(obj)
-
{
-
//need to alert if more than 5 items selected
-
var cNewVal = ""
-
var cCount = 0
-
for(i=0;i<document.getElementById("rDEPT").options.length;i++)
-
{
-
if (document.getElementById("rDEPT").options[i].selected )
-
{
-
cCount += 1;
-
if (cCount > 5)
-
{
-
alert("you have selected more than 5 departments");
-
rePopulate();
-
}else{
-
cNewVal += document.getElementById("rDEPT").options[i].value.substring(0,6)+"~";
-
}
-
}
-
}
-
-
document.getElementById("REVIEWDEPT").value = cNewVal
-
}
-
function rePopulate()
-
{
-
document.getElementById("rDEPT").options.length = 0
-
for (var i = 0; i < rDepartments.length; i++ )
-
{
-
//get the stored array value
-
var cThisValue = rDepartments[i].substring(0,6)
-
//test if this value is in the hidden field REVIEWDEPT
-
var cIsSelected = ( document.getElementById("REVIEWDEPT").value.indexOf(cThisValue)!=-1 ) ? true : false ;
-
//if the value is S for salaried the display text must be properly displayed
-
if (cThisValue == "S")
-
{
-
document.getElementById("rDEPT").options[i] = new Option("Salaried", rDepartments[i], cIsSelected, cIsSelected);
-
}else{
-
document.getElementById("rDEPT").options[i] = new Option(rDepartments[i], rDepartments[i], cIsSelected, cIsSelected);
-
}
-
}
-
}
-
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).
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.
Yes ReviewDept is a data field in my table.
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: 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,...
|
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...
| |
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: 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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |