Allowing Multiple Selections from Drop-down Select List
By Stephen Bucaro
An HTML Select list is a form control used to create a drop-down list of items from which
the user can select. Each item in a select list is defined by an option element. You can
allow the user to select multiple items in the list by setting its multiple attribute.
Code for a select list with its multiple attribute set is shown below.
<select name="mySelect" size="7" multiple="multiple">
<input type="button" value="Get Selection"
The user can select multiple items in the list by pressing the Keyboard Ctrl key,
or by selecting an item and pressing and holding the Shift key while selecting another
item. This second method allows the user to select a range of items from the list.
When the user selects items in a select list, the selected property of the option
is set. The select list options are an array with indexes that start at 0 for the first
option. The code below shows one way to access the indexes of the options set to selected,
and then to access the value property of those options.
var strMsg = "";
for(var i = 0; i < list.length; i++)
strMsg = strMsg + list.options[i].value + "\n";
A select list is a form control, in this code the onclick event of the button
passes the select list to the grabSelection function. There may be other controls
in the form. In that case, you use the form button to pass the entire form.
More Java Script Code:
• Introduction to HTML5 Canvas
• Prevent Automated Form Submissions With Java Script
• Easy Java Script Butterfly Animation
• Java Script Random Password Generator
• Java Script Code to Move Items Between Select Lists
• Easy Expanding Banner Code
• Java Script Code to Factor Numbers
• Round a Float to 4 Digits to the Right of the Decimal Point
• Creating Java Script Dynamic or anonymous Functions at Runtime