Microsoft KB Archive/159757

= How To Read the Selected Items in a SELECT Control =

Article ID: 159757

Article Last Modified on 5/11/2006

-

APPLIES TO


 * Microsoft Visual Basic, Scripting Edition 1.1
 * Microsoft Visual Basic, Scripting Edition 2.0
 * Microsoft Internet Explorer 4.0 128-Bit Edition
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Explorer 5.01
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q159757



1.00 1.10 2.00 WINDOWS kbprg kbhowto



SUMMARY
This article demonstrates how to read what items are selected in a SELECT control on a Web page using Visual Basic Script. Many reference documents indicate that a SELECT control has a VALUE property. However, this is not true. The SELECT control does not have a VALUE property, but the items in the OPTIONS collection of the SELECT control do.

If you are not familiar with the SELECT control, it is similar to the ListBox control in Visual Basic.



MORE INFORMATION
In order to read what item(s) are selected in a SELECT control, you must loop through the elements, and then check if the current element is selected. If it is selected, then you can perform an action.

Use a text editor, such as Notepad.exe, to copy the following sample HTML code into an HTML file: ********* BEGIN Page1.HTM *******************  

Sub ShowVals Dim SelStr For i = 0 to Form1.myselect.length - 1 If Form1.myselect.options(i).selected = True Then selstr = selstr & Form1.myselect.options(i).value & chr(10) End If     Next MsgBox SelStr,,"Selected Item(s)" End Sub





  Option 1 (10) Option 2 (20) Option 3 (30)   

</BODY> </HTML> ********* BEGIN End.HTM  *******************

Program Flow

 * 1) The onClick event of the button on the page calls the ShowVals subroutine.
 * 2) The ShowVals subroutine creates a variable to hold the selected items (SelStr).
 * 3) The "Form1.myselect.length" variable represents the number of items in the list. The For loop uses this value to determine how many iterations it needs in the loop.
 * 4) In each iteration of the loop the program checks to see if the current item is selected.
 * 5) If the current item is selected, then its value is concatenated onto the string described in step 2. "Chr(10)" is also added to the string, and it evaluates to a carriage-return character for cosmetic purposes.
 * 6) A message box displays the string.

<div class="references_section">