Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Checking the selected value of a select object - code


<head>

<script language="javascript">

function setLocation (){

/*
Declare the local variables
*/
var protocol = "http://"//The protocol for the host
var listReference = document.myForm.myUrlList//Get a reference to the select
var selectedIndex = listReference.selectedIndex//Get the selected index /*
Check If the selected index is not the first option in the select
*/
if(selectedIndex != 0){
/*
Declare a variable to hold the value of the selected option's value
*/
var optionValue /*
Get the value from the select by using the listReference
and accessing it's options array
and getting the option of the array the user chose.
*/
optionValue = listReference.options[selectedIndex].value
/*
Change the location of the window to the web adress
using the "http://" protocol
*/
window.location = protocol + optionValue
}
}
</script>
</head>

<!--

Define the form elements in the body of the document
--> <body><!-- Open the body tag -->
<form name="myForm"><!-- Open the form element and give myForm as it's name -->
<!--
Define the select container and give it a name
and register the onChange event and make it call setLocation
-->
<select name="myUrlList" onchange="setLocation()">
<!--
Define the options the user can select
-->
<option selected>Choose one <!-- Make the first one selected with no value --> <!--
Define the other options and set their value to a valid web site address
without the "http://" protocol and provide an explanation for the value
beside the option.
-->
<option value="java.sun.com">Sun's Java
<option value="Apache.org">Apache
<option value="CoolText.com">CoolText
<option value="JShortcut.com">JavaScript Shortcuts
</select><!-- Close the select -->
</form><!-- Close the form -->
</body><!-- Close the body element -->


Close