Click an item in the select control...



On our drop-down box is an "onChange" event..... sending us to the "selectionMade()" function

<SELECT NAME = Select1 onChange = selectionMade()>

 

In the following code we use the "selectedIndex" to get the value of which option was chosen
the + 1 is used because the "selectedIndex" in javascript starts at zero, not at one to number the elements in our option list

<SCRIPT LANGUAGE = JavaScript>

function selectionMade()
{
document.form1.TextBox.value = "You chose selection " + (document.form1.Select1.selectedIndex + 1)
}

</SCRIPT>


Multiple list:

 


<form name="form2" onSubmit="reportMultiple(); return false;">

Okay, so on this form we are heading on to a function called reportMultiple() -- the return false tells the browsers to let go of the function at the end, and allow other processing to continue

function reportMultiple()

{

var options_string = "";

var the_select = window.document.form2.list_1;

for (loop=0; loop < the_select.options.length; loop++)

{

if (the_select.options[loop].selected == true)

{

options_string += the_select.options[loop].text + " ";
// That += is supposed to concatenate onto the text, but doesn't -- just so you see that it isn't working....

}

}

alert("you selected: " + options_string);

}



Pulldown:

List:

change the select

selectedIndex

check the index
Once you know the index number of the selected option, you can find out what it is:
Notice that the arrays are named the same thing as the animals in the array

If we had just done this:
var the_array = the_array_name;
the_array would have equaled the string "Fish" instead of the Fish array

<option>greyhound .
Note the period I used to stretch out my select box. It's sort of cheesy, but it works.