Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript - looping through select options on post back

Status
Not open for further replies.

kittsi

Programmer
Jan 14, 2010
7
US
I have a select options in HTML. On post back I want to loop through this select and get all the values in it. How would I do that in vbscript, I can find anything. I've tried accessing via document.{form}.{select name} but that doesn't work. Any other ideas?

<td align="center">
<select multiple size="15" id="SelectRight">
<option value="one">1
<option value="two">2
</select>
 
document.getElementById("selectRight")
You can use vbs but it will only work in IE. I recommend javascript or PHP. You also need a submit button


Code:
<html>
	<script language="vbscript"> 

		sub doSubmit()
			set objSelect = document.getElementById("selectRight")
			for i = 0 to objSelect.length - 1
				msgbox objSelect.options(i).value
			next
		end sub
	</script>
	
	<body>
		<select id="SelectRight">
			<option value="one">1</option>
			<option value="two">2</option>
		</select>
		
		<input type="button" onClick="doSubmit()" value="submit">
	</body>
[\code]



[URL unfurl="true"]http://raqxtr.sstar.com/caspdoc/html/chapter_5__developer_s_reference.htm[/URL]
 
It's not working. Here's what I have (I'm building the select options dynamically). It's not even getting into the procedure ProcessRequest().:

.....

<td align="center">
<select multiple size="15" id="SelectRight">

<%
set rs2 = GenRecordset("SELECT blah blah blah")

If not (rs2.BOF and rs2.EOF) Then
rs2.MoveFirst

Do Until rs2.EOF
%>
<OPTION VALUE="<%response.Write(trim(rs2("column_name")))%>"><%response.Write(trim(rs2("display_name")))%>
<%
rs2.MoveNext
Loop
End If
rs2.Close

%>



</SELECT>
</td>

.....

<input type="button" name="btnSave" id="btnSave" value="Save" onClick="ProcessRequest()" />

.....

'************** Functions ***********************
Sub ProcessRequest()
Response.Write("Here")

Set sel = Document.getElementById("SelectRight")

For i = 0 To sel.length - 1
' MsgBox sel.options.value
Response.Write(sel.options(i).value)
Next

end Sub
 
If you are using ASP, you would be better served posting here: forum333

Why not put the code in a form, use <input type="submit" ... />, and use Request.Form to retrieve the selected options?
 
It is surrounded by vbscript tags and in a form. I tried accessing the elements through Request.Form, that only gives me the option the user selected in the <select><option>, not all the options within the <select>.

I'll try posting in the ASP forum and see if that helps also, was just assuming since I was trying to access it using vbscript that this might be the correct forum.
 
was just assuming since I was trying to access it using vbscript
It's a fine line. If what you are doing is specifically vbscript, this is the right forum. But if you are using classic ASP, there are ASP-specific objects (such as the Request Object) that have nothing at all to do with vbscript, and the ASP forum may be of more assistance.

It is surrounded by vbscript tags and in a form.
I don't see any <FORM> tags
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top