Solved

Loop with Drop Down Box

Posted on 2008-10-27
5
282 Views
Last Modified: 2012-05-05
I have this javascript script and I am trying to compare values of one drop down to another one to make sure they are not the same selected value.

This is all being generated server side because I never know how many drop downs the user will have on the screen but each selected one has to be uniquely selected.  Can anyone help on how I can append the index passing in and the index from the loop to the script i am using to compare values with?




function CompairMappedData(compareDDIndex) {
 

for(i = 0; i < 52; i++){
 

// we do not want to compair to the same dd that we are passing in

if !(compairDDIndex == i) {
 

if ((document.Form1.Columns_" + compareDDIndex " +.options[document.Form1.Columns_" + compareDDIndex " +.selectedIndex].value) == (document.Form1.Columns_" + i " +.options[document.Form1.Columns_" + i " +.selectedIndex].value)) {
 

alert('You have selected this mapping for another column.\n\nThe mapping for each column must be unique.');
 

document.Form1.Columns_" + compareDDIndex " +.options[document.Form1.Columns_" + compareDDIndex " +.selectedIndex].value = ''
 

}

}

}

}

Open in new window

0
Comment
Question by:Lantrax
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22814019
refer to the following:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
<script type="text/javascript">
//if it returns a non-zero string, there are duplicates
//the return value are indices of list2 that are duplicated in list1
function compareLists(list1,list2){
	var duplicate="";
	for( var i=0, iLimit=list1.options.length; i < iLimit; ++i)
	{
		for( var j=0, jLimit=list2.options.length; j < jLimit; ++j)
		{
			if( list1.options[i].selected && list2.options[j].selected && list1.options[i].value== list2.options[j].value )
				duplicate +=  ","+j;
		}
	}
return duplicate.substring(1);
}
</script>
</head>
<body>
<select name="alpha" id='alpha' multiple="multiple" onchange="alert( compareLists(this,beta) )"><option value='1'>a</option><option value='2'>b</option><option value='3'>c</option><option value='4'>d</option></select>
<select name="beta" id='beta' multiple="multiple"  onchange="alert( compareLists(alpha,this) )"><option value='1'>a</option><option value='2'>b</option><option value='3'>c</option><option value='4'>d</option></select>
</body>
</html>

Open in new window

0
 

Author Comment

by:Lantrax
ID: 22814107
That script is a bit different from what I am trying to accomplish. The number of drop down boxes changes based on the user loading the page. There may be 50 or 100 drop downs on a page. Each one has to be unique. So I have server side code to loop through and generate the index to loop to in the for each loop. I pass in the drop down box being changed but the function needs to loop through all drop down boxes on the page and verify that non of them have the same option selected.

j
0
 
LVL 82

Accepted Solution

by:
hielo earned 125 total points
ID: 22814189
try:
function CompairMappedData(compareDDIndex) {

	for(i = 0; i < 52; i++)

	{

		// we do not want to compair to the same dd that we are passing in

		if( !compairDDIndex == i )

		{

			var sel = document.Form1["Columns_" + compareDDIndex];

			var sel2= document.Form1["Columns_" + i];

			if( sel.options[ sel.selectedIndex ].value == sel2.options[ sel2.selectedIndex ].value ) 

			{

				alert('You have selected this mapping for another column.\n\nThe mapping for each column must be unique.');

				sel.options[ sel.selectedIndex ].value = '';

			}

		}

	}

}

Open in new window

0
 

Author Comment

by:Lantrax
ID: 22814477
Thank you very much. I made a few changes although you put me on the right track. Thank you very much.



function CompareMappedData(compareDDIndex) {
 

    	var sel = document.Form1["Columns_" + compareDDIndex];

		// we do not want to compare if the selected index = 0 because that is the Ignore Column option.

 		if (sel.selectedIndex != 0 ) 

		{

			for (i = 0; i < 52; i++)

			{

					// we do not want to compare to the same dd that we are passing in

					if (parseInt(compareDDIndex) != parseInt(i) )

					{

							var sel2= document.Form1["Columns_" + i];

							if ( sel.options[ sel.selectedIndex ].value == sel2.options[ sel2.selectedIndex ].value ) 

							{

									alert('You have selected this mapping for another column.\n\nThe mapping for each column must be unique.');

									sel.selectedIndex = 0;

									break;

							}

					}

			}

		}

}

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 22814502
glad to help
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now