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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Funtion to addOption to select not working 1

Status
Not open for further replies.

frumpus

Programmer
Aug 1, 2005
113
US
I'm far from an expert in Javascript but I have done this before and it's usually pretty simple. Can a trained eye see what I've done wrong here? Nothing happens when I change the 'state' in the form.

Code:
<script language="javascript">

function newState()
{

	removeAllOptions(document.artic.school);
	addOption(document.artic.school, "", "");
	
			if(document.artic.state.value == 'IL')
			{
						addOption(document.artic.school, "1005", "College Of Lake County"); 
						addOption(document.artic.school, "1019", "John Wood Community College"); 
						addOption(document.artic.school, "0964", "Kaskaskia College"); 
						addOption(document.artic.school, "R00573", "Keller Graduate School Of Mgmt"); 
						addOption(document.artic.school, "1064", "Loyola University Of Chicago"); 
						addOption(document.artic.school, "1076", "Mckendree College"); 
						addOption(document.artic.school, "0954", "Southwestern Illinois College"); 
			}
			if(document.artic.state.value == 'IA')
			{
						addOption(document.artic.school, "1272", "Des Moines Area Comm C-Ankeny"); 
						addOption(document.artic.school, "1279", "Des Moines Area Comm C-Urban"); 
						addOption(document.artic.school, "1314", "Graceland College"); 
						addOption(document.artic.school, "1309", "Hawkeye Community College"); 
						addOption(document.artic.school, "1269", "Indian Hills Comm Colg-Ottumwa"); 
						addOption(document.artic.school, "1275", "Kirkwood Community College"); 
						addOption(document.artic.school, "1330", "Luther College"); 
						addOption(document.artic.school, "1369", "Western Iowa Tech Comm College"); 
			}

			if(document.artic.state.value == 'NE')
			{
						addOption(document.artic.school, "2444", "Creighton University"); 
						addOption(document.artic.school, "2447", "Metropolitan Comm College - Ne"); 
						addOption(document.artic.school, "2491", "Mid-Plains Community College"); 
						addOption(document.artic.school, "2474", "Nebraska Wesleyan University"); 
						addOption(document.artic.school, "4787", "Southeast Cc - Lincoln Campus"); 
						addOption(document.artic.school, "2468", "Univ Of Nebraska At Kearney"); 
						addOption(document.artic.school, "2482", "Univ Of Nebraska At Lincoln"); 
						addOption(document.artic.school, "2464", "Univ Of Nebraska At Omaha"); 
			}
			
			if(document.artic.state.value == 'OK')
			{
						addOption(document.artic.school, "3431", "Oklahoma City Comm College"); 
						addOption(document.artic.school, "3424", "Oklahoma State University"); 
						addOption(document.artic.school, "3441", "Tulsa Community College"); 
						addOption(document.artic.school, "3442", "University Of Oklahoma"); 
			}
}
</script>
</head>
<body>


                    <form name="artic" method="post" action="articprint.asp">
                    <select name="state" style="width:200px" onchange="newState();" >
                    	<option value=""></option>
                        <option value="IL">Illinois</option>
                        <option value="IA">Iowa</option>
                        <option value="NE">Nebraska</option>
                        <option value="OK">Oklahoma</option>
                    </select>
                    <select name="school" style="width:200px" >
                    </select>
                    <input type="submit" value="Go"  />
                    </form>

</body>
</html>
 
hmm....

I've never had to declare that before and always though it was built in. After double checking other sites where I've used it and it works fine I don't see it ever declared anywhere.
 
gah, ok I found the addOption function. Sorry I missed that the first time through. However, I have now added it to this page and it's still not working. I am now using this code and the behavior is the same.

Code:
<script language="javascript">

function addOption(selectbox,value,text)
{
	var optn = document.createElement("OPTION");
	optn.value = value;
	optn.text = text;
	selectbox.options.add(optn);
}

function newState()
{

	removeAllOptions(document.artic.school);
	addOption(document.artic.school, "", "");
	
			if(document.artic.state.value == 'IL')
			{
						addOption(document.artic.school, "1005", "College Of Lake County"); 
						addOption(document.artic.school, "1019", "John Wood Community College"); 
						addOption(document.artic.school, "0964", "Kaskaskia College"); 
						addOption(document.artic.school, "R00573", "Keller Graduate School Of Mgmt"); 
						addOption(document.artic.school, "1064", "Loyola University Of Chicago"); 
						addOption(document.artic.school, "1076", "Mckendree College"); 
						addOption(document.artic.school, "0954", "Southwestern Illinois College"); 
			}
			if(document.artic.state.value == 'IA')
			{
						addOption(document.artic.school, "1272", "Des Moines Area Comm C-Ankeny"); 
						addOption(document.artic.school, "1279", "Des Moines Area Comm C-Urban"); 
						addOption(document.artic.school, "1314", "Graceland College"); 
						addOption(document.artic.school, "1309", "Hawkeye Community College"); 
						addOption(document.artic.school, "1269", "Indian Hills Comm Colg-Ottumwa"); 
						addOption(document.artic.school, "1275", "Kirkwood Community College"); 
						addOption(document.artic.school, "1330", "Luther College"); 
						addOption(document.artic.school, "1369", "Western Iowa Tech Comm College"); 
			}

			if(document.artic.state.value == 'NE')
			{
						addOption(document.artic.school, "2444", "Creighton University"); 
						addOption(document.artic.school, "2447", "Metropolitan Comm College - Ne"); 
						addOption(document.artic.school, "2491", "Mid-Plains Community College"); 
						addOption(document.artic.school, "2474", "Nebraska Wesleyan University"); 
						addOption(document.artic.school, "4787", "Southeast Cc - Lincoln Campus"); 
						addOption(document.artic.school, "2468", "Univ Of Nebraska At Kearney"); 
						addOption(document.artic.school, "2482", "Univ Of Nebraska At Lincoln"); 
						addOption(document.artic.school, "2464", "Univ Of Nebraska At Omaha"); 
			}
			
			if(document.artic.state.value == 'OK')
			{
						addOption(document.artic.school, "3431", "Oklahoma City Comm College"); 
						addOption(document.artic.school, "3424", "Oklahoma State University"); 
						addOption(document.artic.school, "3441", "Tulsa Community College"); 
						addOption(document.artic.school, "3442", "University Of Oklahoma"); 
			}
}
</script>
</head>
<body>


                <form name="artic" method="post" action="articprint.asp">
                <select name="state" style="width:200px" onchange="newState();" >
                   	<option value=""></option>
                    <option value="IL">Illinois</option>
                    <option value="IA">Iowa</option>
                    <option value="NE">Nebraska</option>
                    <option value="OK">Oklahoma</option>
                </select>
                    <select name="school" style="width:200px" >
                    </select>
                    <input type="submit" value="Go"  />
                    </form>

</body>
</html>
 
Ah, of course. Bingo.

thanks a ton!

Code:
function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}
 
I would also strongly suggest you use the error console of whichever browser you are using to test this to debug.

The undeclared function errors should appear there readily.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top