vanacoro
04-14-2005, 05:18 PM
I found this code and incorporated into my page. It removes an option if selected in Option area 1 so it cannot be selected in Option area 2.
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var OptLstTxt = new Array;
var OptLstVal = new Array;
var OptLen = 0;
function NoDupl(SelObjFrom, SelObjTo) {
var OldToVal = SelObjTo.options[SelObjTo.selectedIndex].value;
if (OptLen == 0) {
OptLen = SelObjFrom.length;
for (var i = 1; i < OptLen; i++) {
OptLstTxt[i] = SelObjFrom.options[i].text;
OptLstVal[i] = SelObjFrom.options[i].value;
}
}
var j = 1;
for (var i = 1; i < OptLen; i++) {
if (OptLstVal[i] != SelObjFrom.options[SelObjFrom.selectedIndex].value) {
if (j == SelObjTo.length) {
SelObjTo.options[j] = new Option(OptLstTxt[i]);
}
else {
SelObjTo.options[j].text = OptLstTxt[i];
}
SelObjTo.options[j].value = OptLstVal[i];
if (OptLstVal[i] == OldToVal) {
SelObjTo.selectedIndex = j;
}
j++;
}
}
if (SelObjTo.length > j)
SelObjTo.options[(SelObjTo.length - 1)] = null;
}
// End -->
</script>
</HEAD>
<BODY>
<form method="POST" name="MForm">
Try to select the same color:
<br>
<select name="Color_1" onChange="NoDupl(this,document.MForm.Color_2)">
<option selected value=''>Select your first prefered color</option>
<option value='R'>Red</option>
<option value='J'>Yellow</option>
<option value='G'>Green</option>
<option value='B'>Blue</option>
</select>
<select name="Color_2" onChange="NoDupl(this,document.MForm.Color_1)">
<option selected value=''>Select Second Prefered color</option>
<option value='R'>Red</option>
<option value='J'>Yellow</option>
<option value='G'>Green</option>
<option value='B'>Blue</option>
</select>
</form>
My question is: since I have three pulldowns I'm trying to keep from being duplicative, how can I modify this script to do that ? Obviously, I need to add another input section, like this:
<select name="Color_3" onChange="NoDupl(this,document.MForm.Color_1)">
<option selected value=''>Select Second Prefered color</option>
<option value='R'>Red</option>
<option value='J'>Yellow</option>
<option value='G'>Green</option>
<option value='B'>Blue</option>
</select>
But, how do I include the onChange function into section #3 ? Also, do I need to do anything to the script, like change the var i =1 to a 2 ?
Thanks again. I'm learning tons communicating with you.
TK Vanacoro :thumbsup:
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var OptLstTxt = new Array;
var OptLstVal = new Array;
var OptLen = 0;
function NoDupl(SelObjFrom, SelObjTo) {
var OldToVal = SelObjTo.options[SelObjTo.selectedIndex].value;
if (OptLen == 0) {
OptLen = SelObjFrom.length;
for (var i = 1; i < OptLen; i++) {
OptLstTxt[i] = SelObjFrom.options[i].text;
OptLstVal[i] = SelObjFrom.options[i].value;
}
}
var j = 1;
for (var i = 1; i < OptLen; i++) {
if (OptLstVal[i] != SelObjFrom.options[SelObjFrom.selectedIndex].value) {
if (j == SelObjTo.length) {
SelObjTo.options[j] = new Option(OptLstTxt[i]);
}
else {
SelObjTo.options[j].text = OptLstTxt[i];
}
SelObjTo.options[j].value = OptLstVal[i];
if (OptLstVal[i] == OldToVal) {
SelObjTo.selectedIndex = j;
}
j++;
}
}
if (SelObjTo.length > j)
SelObjTo.options[(SelObjTo.length - 1)] = null;
}
// End -->
</script>
</HEAD>
<BODY>
<form method="POST" name="MForm">
Try to select the same color:
<br>
<select name="Color_1" onChange="NoDupl(this,document.MForm.Color_2)">
<option selected value=''>Select your first prefered color</option>
<option value='R'>Red</option>
<option value='J'>Yellow</option>
<option value='G'>Green</option>
<option value='B'>Blue</option>
</select>
<select name="Color_2" onChange="NoDupl(this,document.MForm.Color_1)">
<option selected value=''>Select Second Prefered color</option>
<option value='R'>Red</option>
<option value='J'>Yellow</option>
<option value='G'>Green</option>
<option value='B'>Blue</option>
</select>
</form>
My question is: since I have three pulldowns I'm trying to keep from being duplicative, how can I modify this script to do that ? Obviously, I need to add another input section, like this:
<select name="Color_3" onChange="NoDupl(this,document.MForm.Color_1)">
<option selected value=''>Select Second Prefered color</option>
<option value='R'>Red</option>
<option value='J'>Yellow</option>
<option value='G'>Green</option>
<option value='B'>Blue</option>
</select>
But, how do I include the onChange function into section #3 ? Also, do I need to do anything to the script, like change the var i =1 to a 2 ?
Thanks again. I'm learning tons communicating with you.
TK Vanacoro :thumbsup: