select with same data/options
-
hi guys, i have six(6) select controls all with values 1-6. when i click submit in javascript i would like to check that a select's value has not already been selected by the other five (5) selects. can anyone please help me how i can do this, i have tried logic after logic and yet still no joy at all...please help me. i tried:
//check template orders var ordercntr = false;//just a control key var partial = '';//i'm appending the selects ids to this variable on each iteration. var orderfinal = new Array();//will add the successful values of my selects(inside my if statement) for (l = 0; l < order.length; l++) { //looping through an array that contains selects ids(order) //select ids have the form: sel\_0, sel\_1, sel\_2, sel\_3, sel\_4, sel\_5 (there six selects in total) partial += order\[l\] + '~';//appending to my partial variable, the current select's id var d = document.getElementById(order\[l\]).value;//getting the value of the select if (partial.search(d) < 1) { //i'm searching for whether the selected value e.g. 1 has already been appended to my //partial variable 'partial' which at first iteration has a value of sel\_0~, second iteration=sel\_0~sel\_1 orderfinal\[l\] = d; ordercntr = true; } else { ordercntr = false; break; } }
The logic works like a charm if the selected values in the selects are in the order 1-6 But fails badly if lets say the second(2) select has the value 5(or something else other than 2) i know i have to revisit the entire logic but i have tried and now i'm asking for help before i try forever. I will appreciate any help. Morgs
-
hi guys, i have six(6) select controls all with values 1-6. when i click submit in javascript i would like to check that a select's value has not already been selected by the other five (5) selects. can anyone please help me how i can do this, i have tried logic after logic and yet still no joy at all...please help me. i tried:
//check template orders var ordercntr = false;//just a control key var partial = '';//i'm appending the selects ids to this variable on each iteration. var orderfinal = new Array();//will add the successful values of my selects(inside my if statement) for (l = 0; l < order.length; l++) { //looping through an array that contains selects ids(order) //select ids have the form: sel\_0, sel\_1, sel\_2, sel\_3, sel\_4, sel\_5 (there six selects in total) partial += order\[l\] + '~';//appending to my partial variable, the current select's id var d = document.getElementById(order\[l\]).value;//getting the value of the select if (partial.search(d) < 1) { //i'm searching for whether the selected value e.g. 1 has already been appended to my //partial variable 'partial' which at first iteration has a value of sel\_0~, second iteration=sel\_0~sel\_1 orderfinal\[l\] = d; ordercntr = true; } else { ordercntr = false; break; } }
The logic works like a charm if the selected values in the selects are in the order 1-6 But fails badly if lets say the second(2) select has the value 5(or something else other than 2) i know i have to revisit the entire logic but i have tried and now i'm asking for help before i try forever. I will appreciate any help. Morgs
there are several ways to do this. here are a few: 1. compare 2 with 1; then 3 with 1 and 2; then... 2. create a bit mask where each bit corresponds to a value (bit1=value 1, bit2=value 2, bit3= value 3,...); start at zero, then OR with the bit value, in pseudo-code
bitmask=bitmask | (1< and finally check the final value is what it should be: 0x7E 3. similar to #2, use a bit mask; before ORing in a new bit, check the bit is still zero; if it isn't, there is a duplicate. 4. same as #2 or #3, however use a string instead of a bit mask. 5. same as #2, however use an array, have it sorted, then compare with what it should be. :) Luc Pattyn [[Forum Guidelines]](/KB/scrapbook/ForumGuidelines.aspx) [[My Articles]](http://www.perceler.com/articles1.php) Nil Volentibus Arduum Please use [**<PRE>**](/Tips/42071/Use-PRE-tags.aspx) tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
there are several ways to do this. here are a few: 1. compare 2 with 1; then 3 with 1 and 2; then... 2. create a bit mask where each bit corresponds to a value (bit1=value 1, bit2=value 2, bit3= value 3,...); start at zero, then OR with the bit value, in pseudo-code
bitmask=bitmask | (1< and finally check the final value is what it should be: 0x7E 3. similar to #2, use a bit mask; before ORing in a new bit, check the bit is still zero; if it isn't, there is a duplicate. 4. same as #2 or #3, however use a string instead of a bit mask. 5. same as #2, however use an array, have it sorted, then compare with what it should be. :) Luc Pattyn [[Forum Guidelines]](/KB/scrapbook/ForumGuidelines.aspx) [[My Articles]](http://www.perceler.com/articles1.php) Nil Volentibus Arduum Please use [**<PRE>**](/Tips/42071/Use-PRE-tags.aspx) tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Hi Luc, Thanks for your suggestion, i found dodgy solution though. 1. I put all my selects ids in an array (e.g. ItemOrder) 2. i loop through the ItemOrder likse so;;
var count=0;
var isMoreThanTwo=false;
for(i=0;i1 then user chose the current select's value in more than one select
//otherwise count will always remain 1 if this select's value is unique
if(count>1){isMoreThanTwo=true;break;}} if(isMoreThanTwo)break;
}
if(count<=1)
{
//user chose a unique value for each select
}
else
{
//error, user has selected duplicate values in the selects!
}The above works like a charm, might not be the best solution but does just the trick Thanks, Morgs
-
Hi Luc, Thanks for your suggestion, i found dodgy solution though. 1. I put all my selects ids in an array (e.g. ItemOrder) 2. i loop through the ItemOrder likse so;;
var count=0;
var isMoreThanTwo=false;
for(i=0;i1 then user chose the current select's value in more than one select
//otherwise count will always remain 1 if this select's value is unique
if(count>1){isMoreThanTwo=true;break;}} if(isMoreThanTwo)break;
}
if(count<=1)
{
//user chose a unique value for each select
}
else
{
//error, user has selected duplicate values in the selects!
}The above works like a charm, might not be the best solution but does just the trick Thanks, Morgs
that looks OK, except
if(d==document.getElementById(ItemOrder[i]).value)
should use a, not i. Keeping the approach, the code can be further improved by: - reducing the inner loop tofor(a=i+1;a<ItemOrder.length;a++)
; - dropping thecount
variable - renaming theisMoreThanTwo
variable tothereAreDuplicates
something along these lines:var thereAreDuplicates=false;
for(i=0;i<ItemOrder.length;i++) {
var d=document.getElementById(ItemOrder[i]).value;
for(a=i+1;a<ItemOrder.length;a++) {
if(d==document.getElementById(ItemOrder[a]).value) thereAreDuplicates=true;
}
if(thereAreDuplicates)break;
}:) .
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
that looks OK, except
if(d==document.getElementById(ItemOrder[i]).value)
should use a, not i. Keeping the approach, the code can be further improved by: - reducing the inner loop tofor(a=i+1;a<ItemOrder.length;a++)
; - dropping thecount
variable - renaming theisMoreThanTwo
variable tothereAreDuplicates
something along these lines:var thereAreDuplicates=false;
for(i=0;i<ItemOrder.length;i++) {
var d=document.getElementById(ItemOrder[i]).value;
for(a=i+1;a<ItemOrder.length;a++) {
if(d==document.getElementById(ItemOrder[a]).value) thereAreDuplicates=true;
}
if(thereAreDuplicates)break;
}:) .
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Thanks Luc, that looks pretty aswell, Oops your first comment was just a typo when i was writing at the forum otherwise i have it correct in my code.
-
hi guys, i have six(6) select controls all with values 1-6. when i click submit in javascript i would like to check that a select's value has not already been selected by the other five (5) selects. can anyone please help me how i can do this, i have tried logic after logic and yet still no joy at all...please help me. i tried:
//check template orders var ordercntr = false;//just a control key var partial = '';//i'm appending the selects ids to this variable on each iteration. var orderfinal = new Array();//will add the successful values of my selects(inside my if statement) for (l = 0; l < order.length; l++) { //looping through an array that contains selects ids(order) //select ids have the form: sel\_0, sel\_1, sel\_2, sel\_3, sel\_4, sel\_5 (there six selects in total) partial += order\[l\] + '~';//appending to my partial variable, the current select's id var d = document.getElementById(order\[l\]).value;//getting the value of the select if (partial.search(d) < 1) { //i'm searching for whether the selected value e.g. 1 has already been appended to my //partial variable 'partial' which at first iteration has a value of sel\_0~, second iteration=sel\_0~sel\_1 orderfinal\[l\] = d; ordercntr = true; } else { ordercntr = false; break; } }
The logic works like a charm if the selected values in the selects are in the order 1-6 But fails badly if lets say the second(2) select has the value 5(or something else other than 2) i know i have to revisit the entire logic but i have tried and now i'm asking for help before i try forever. I will appreciate any help. Morgs
-
{1,1,1,6,6,6} doesn't contain duplicates? :confused:
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
{1,1,1,6,6,6} doesn't contain duplicates? :confused:
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.