Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem Removing Values from Select 1

Status
Not open for further replies.

Ebow

Programmer
Oct 11, 2002
24
IE
Hey,

I have 2 select menus, and depending on what is selected in the first one, the values change in the second one. This all works fine, but if someone changes the first menu to the default option, I want to clear all options from the second menu.

The function is as follows:

Code:
function updateResorts()
{
	
	var dest = document.BBForm.Country.options[document.BBForm.Country.selectedIndex].value;

	if (IsNumeric(dest))
	{

		var btDestinations = new Array();
	
		btDestinations[1] = new Array();
		btDestinations[1][1] = "Benidorm 1";
		btDestinations[1][2] = "Benidorm 2";
		btDestinations[1][3] = "Benidorm 3";
		btDestinations[1][4] = "Benidorm 4";
		btDestinations[1][5] = "Benidorm 5";
		btDestinations[1][6] = "Benidorm 6";
		btDestinations[1][7] = "Benidorm 7";
		btDestinations[1][8] = "Benidorm 8";
		
		btDestinations[2] = new Array();
		btDestinations[2][1] = "Benalmadena, 1";
		btDestinations[2][2] = "Marbella, 2";
		btDestinations[2][3] = "Torremolinos, 3";
		
		btDestinations[3] = new Array();
		btDestinations[3][1] = "Caleta de Fuste, 1";
		btDestinations[3][2] = "Corralejo, 2";
	
		var ccDest = btDestinations[dest].length;
		document.BBForm.Resort.options[1] = null;
	
		document.BBForm.Resort.options[0] = new Option("Please Select Resort","");
		for(i = 1; i <  ccDest; i++) 
		{
			var dtValues=btDestinations[dest][i].split(", ");
			document.BBForm.Resort.options[i] = new Option(dtValues[0],dtValues[1]);
		}
		
	}
	
	else
	{
		document.BBForm.Resort.selectedIndex = 0;

		var killOptions = document.BBForm.Resort.options.length;

		for (j=1;j<killOptions;j++)
		{
			document.BBForm.Resort.remove(j);
		}
		
	}
	
}

The code in the else statement is supposed to clear the values of the second drop down, but no matter what, it always leaves the even elements in the list. Any ideas how I could fix this?

A working example of the code can be seen here:
Thanks!
 

Change this code:

Code:
        var killOptions = document.BBForm.Resort.options.length;

        for (j=1;j<killOptions;j++)
        {
            document.BBForm.Resort.remove(j);
        }

to this:

Code:
var sel = document.forms['BBForm'].elements['Resort'];
while (sel.options.length) sel.remove(0);

Hope this helps,
Dan

 

Just so you know the reason all even items were being left...

When your loop removed item 0, item 1 became item 0, item 2 became item 1, and so on. The next iteration of the loop removed item 1 (which was item 2), so skipped every other item.

The new code always removes the first item until there are no more left.

Dan

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top