Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Duplicate values in drop down list box

Reply
Thread Tools

Duplicate values in drop down list box

 
 
marx
Guest
Posts: n/a
 
      11-17-2003
I have a bit of a problem and any help would be much appreciated.

Problem: I have two dropdown list boxes with same data(all data
driven).
These are used for two separate entries.
For every entry you cannot choose the same value twice.
For example, I cannot choose for entry 1 the same
value in both selection boxes (gqCategory1Entry1 and
gqCategory2Entry1)

This part works.
The second entry is the problem: When I choose a value for Entry
Two that is the same as in entry one it thinks that "Dubplicate
Divisons have been selected").

WHen in fact these are two separate entries.

Code:
<head>
//Enry number one...no Duplicates
function check_selection(elt){
//check for duplicate selections
var form=elt.form;
var name=elt.name;
var index=elt.selectedIndex
//loop through all form elements
for(var i=0;i<form.length;i++){
var_name=form.elements[i].name;
var_index=form.elements[i].selectedIndex;
if(var_name.substring(0,16)!='gqCategory'){
// if form element is not the current element
// and the division name is the same, raise
//error message
if(var_name!=name&&index!=0&&var_index==index){
alert(var_name);
alert("Duplicate divisions selected! Please choose again.");
elt.selectedIndex=0;
elt.focus();
return false;
}
}
}
return true;
}


</head>

Entry one:
<select name="gqCategory1Entry1" maxlength="48" tabindex = 20
onChange="check_selection(this)" maxlength="48">
<option value="-1"> ---Select a Category---
<option value=1.1 > Division - 1.1 - Internet Sites
<option value=1.2 > Division - 1.2 - Intranet Sites
<option value=1.3 > Division - 1.3 - Interactive communication
</select>
<select name="gqCategory2Entry1" maxlength="48" tabindex = 20
onChange="check_selection(this)" maxlength="48">
<option value="-1"> ---Select a Category---
<option value=1.1 > Division - 1.1 - Internet Sites
<option value=1.2 > Division - 1.2 - Intranet Sites
<option value=1.3 > Division - 1.3 - Interactive communication
</select>
------------
Entry TWO:
<select name="gqCategory1Entry2" maxlength="48" tabindex = 20
onChange="check_selection(this)" maxlength="48">
<option value="-1"> ---Select a Category---
<option value=1.1 > Division - 1.1 - Internet Sites
<option value=1.2 > Division - 1.2 - Intranet Sites
<option value=1.3 > Division - 1.3 - Interactive communication
</select>
<select name="gqCategory2Entry2" maxlength="48" tabindex = 20
onChange="check_selection(this)" maxlength="48">
<option value="-1"> ---Select a Category---
<option value=1.1 > Division - 1.1 - Internet Sites
<option value=1.2 > Division - 1.2 - Intranet Sites
<option value=1.3 > Division - 1.3 - Interactive communication
</select>
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      11-17-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (marx) writes:

> I have a bit of a problem and any help would be much appreciated.
>
> Problem: I have two dropdown list boxes with same data(all data
> driven).


You have two select elements with identical options.

> These are used for two separate entries.


I.e., you have *four* select elements with identical options,
grouped into two "entries".

> For every entry you cannot choose the same value twice.
> For example, I cannot choose for entry 1 the same
> value in both selection boxes (gqCategory1Entry1 and
> gqCategory2Entry1)
>
> This part works.
> The second entry is the problem: When I choose a value for Entry
> Two that is the same as in entry one it thinks that "Dubplicate
> Divisons have been selected").


So the code checks all select elements, not only the ones in the same
"entry".

> WHen in fact these are two separate entries.
>
> Code:
> <head>
> //Enry number one...no Duplicates
> function check_selection(elt){
> //check for duplicate selections
> var form=elt.form;
> var name=elt.name;
> var index=elt.selectedIndex
> //loop through all form elements
> for(var i=0;i<form.length;i++){
> var_name=form.elements[i].name;
> var_index=form.elements[i].selectedIndex;


These variables are not declared, so they become global variables.
No need for that. Put a "var" in front.

> if(var_name.substring(0,16)!='gqCategory'){


You only check that they have the same first 16 characters. That misses
the distinction between entries, which is much later in the name.

Example names:
gqCategory1Entry2
gqCategory1Entry1

The entry number is past the first 16 characters, and is never checked.

In case you ever need more than 9 or 10 categories or entries, let's
make this work for any number:

---
function check_selection(elt){
var gqCategoryRE = /^gqCategory(\d+)Entry(\d+)$/;
var index = elt.selectedIndex;
if (index == 0) {return true;} // always legal
var match = elt.name.match(gqCategoryRE);
if (!match) { return; } // not a gqCategory at all.
var category = +match[1];
var entry = +match[2];

var elems = elt.form.elements;
for (var i=0;i<elems.length;i++) {
if (elems[i] == elt) {continue;} // don't test self
match=elems[i].name.match(gqCategoryRE);
if ( match && entry == +match[2] && // same entry
index == elems[i].selectedIndex) { // same selectedIndex
alert("Duplicate division selected! Please choose again.");
elt.selectedIndex = 0;
elt.focus(0);
return false;
}
}
return true;
}
---

Tested in Opera 7 with the supplied select elements.
/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Affecting a dynamically created drop down from another dynamically created drop down. msimmons ASP .Net 0 07-16-2009 03:17 PM
retrive preselected value in second drop down list from the first drop down list weiwei ASP .Net 0 01-05-2007 07:29 PM
Copying items from a Drop Down List to a List Box? mc ASP .Net 5 06-26-2006 03:38 AM
Auto Drop down a Drop Down List xxbmichae1@supergambler.com Javascript 5 11-23-2005 01:35 AM
New to .NET, can I have one drop down box control the data of another drop down box using a database? SirPoonga ASP .Net 2 01-07-2005 10:44 PM



Advertisments