Click to See Complete Forum and Search --> : Detecting selected values in a multiple <option select> listbox


geuis
04-13-2003, 08:03 PM
I have the following code which lets me find the value selected in a pop-up window.
<html>
<script language="javascript">
if (document.form1.customer_type.options[document.form1.customer_type.selectedIndex].value =="")
{
alert("Please select a customer type.")
document.form1.customer_type.focus();
return false;
}
</script>

<body>
<form>
<select size="1" name="customer_type" tabindex="9" id="required">
<option value="" selected>Customer Type
<option value="Good">Good
<option value="Bad">Bad
<option value="Ugly">Ugly

</select>

</form>
</body>

</html>

That code works fine. However, I have a small variation which I'm not sure how to code.

<select name="outbound" size="5" multiple id="required">
<option selected>Customer Type
<option value="Goodly">Goodly
<option value="Badly">Badly
<option value="Uglyly">Uglyly
</select>

I am not sure how to code the javascript to detect multiple selected values. Any help is appreciated.

devinabox
04-13-2003, 08:31 PM
This is a function that will put all of the selected values into a javascript array. I put it in the "onChange" event of the multiple select element.

function getSelectedValues (select)
{
var r = new Array();
for (var i = 0; i < select.options.length; i++)
if (select.options[i].selected)
{
r[r.length] = select.options[i].value;
}
return r;
}

...

<select name="outbound" size="5" multiple id="required" onChange="myArray=getSelectedValues(this)">
<option selected>Customer Type
<option value="Goodly">Goodly
<option value="Badly">Badly
<option value="Uglyly">Uglyly
</select>

geuis
04-14-2003, 12:52 AM
thanks. I'll give it a try tomorrow at work.

Geuis