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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve select box text and value

Status
Not open for further replies.

itchyII

MIS
Apr 10, 2001
167
Hi Everyone,
I am trying to retrieve both the value and the text of a select box (this data will be placed in an email and sent). I know how to get the value of the select box but I can't seem to find anywhere how to get the text as well. Is it possible. Here is a snippit of my code...

My VBscript statement:

strSurveyOne = Trim(Request.Form("selSurveyOne")) & Empty
'this gets the value just fine...but no text

My HTML code:

<SELECT tabindex = "1" style="WIDTH: 175px" name="selSurveyOne" onchange=addComment("commentOne",this.options[this.selectedIndex].value)>
<OPTION value = "" selected>Please select</OPTION>
<OPTION value = 5>Always</OPTION>
<OPTION value = 4>Frequently</OPTION>
<OPTION value = 3>Sometimes</OPTION>
<OPTION value = 2>Never</OPTION> <OPTION value = 1>No Comment</OPTION>
</SELECT>

So basically, if the user has selected 'Frequently', I want strSurveyOne to contain "4 - Frequently". Can it be done without an enormous amount of code?
Thanks,
ItchyII
 
this should be in the asp forum.

Code:
<SELECT tabindex = "1" style="WIDTH: 175px" name="selSurveyOne" onchange=addComment("commentOne",this.options[this.selectedIndex].value)>
    <OPTION value="" selected>Please select</OPTION>
    <OPTION value="5-Always">Always</OPTION>
    <OPTION value="4-Frequently">Frequently</OPTION>
    <OPTION value="3-Sometimes">Sometimes</OPTION>
    <OPTION value="2-Never">Never</OPTION>
    <OPTION value="1-No Comment">No Comment</OPTION>
</SELECT>


if you need to seperate the values back out again - just split on (-) or use left and the position of the (-)
 
I wasn't very happy with other replies to this question because I don't think the request was well enough comprehended, so I did my own checking on methods usable for the <select> form element. Turns out you can use options(selectedindex).text method in your script to acquire the value of the text within the <option>s of your <select> boxes.

Here's how I did it with the help of Microsoft Knowledge Base Article - 197959 for Front Page:
=========================================
<html><head></head><title></title>
<script language=vbscript>
<!--
sub get_text_index()
dim aaa
aaa=document.form1.select1.selectedindex
b=document.form1.select1.options(aaa).value
c=document.form1.select1.options(aaa).text
d=b & " - " & c
document.form1.text1.value=d
end sub
-->
</script>
<body>
<form name=form1 method=post action="self.asp"><!-- unnecessary but I placed this here for script's-sake above -->
<select name="select1" value="0" style="width:100px" onclick="vbscript:get_text_index()">
<option selected value="">---select one---
<option value="1">Test A
<option value="2">Test B
<option value="3">Test C</select>
<input type="text" name=text1 width=50>
</form>
</body>
</html>
=========================================
Not the best programmer, but I hope that helps!
-Jeff
 
So, for your code (reshown below):

You may be able to concatenate right from the 'get-go' like so:

<SELECT tabindex = "1" style="WIDTH: 175px" name="selSurveyOne" onchange=addComment("commentOne",this.options[this.selectedIndex].value & " - " & this.options[this.selectedIndex].text)>

I haven't tested this, but if so, then your variable 'strSurveyOne' in your .asp request will have the completed value without any need to parse it all in to the value of a hidden text box, as was done in my example, etc.
=======================
My VBscript statement:

strSurveyOne = Trim(Request.Form("selSurveyOne")) & Empty
'this gets the value just fine...but no text

My HTML code:

<SELECT tabindex = "1" style="WIDTH: 175px" name="selSurveyOne" onchange=addComment("commentOne",this.options[this.selectedIndex].value)>
<OPTION value = "" selected>Please select</OPTION>
<OPTION value = 5>Always</OPTION>
<OPTION value = 4>Frequently</OPTION>
<OPTION value = 3>Sometimes</OPTION>
<OPTION value = 2>Never</OPTION> <OPTION value = 1>No Comment</OPTION>
</SELECT>

So basically, if the user has selected 'Frequently', I want strSurveyOne to contain "4 - Frequently". Can it be done without an enormous amount of code?
Thanks,
ItchyII
 
How does that work with javascript turned off?

but to optimise ur code further try this:

Code:
<select onChange="document.forms["formname"].elements["fieldname"].value=this.options[this.selectedIndex].value[b]+[/b]' - '[b]+[/b]this.options[this.selectedIndex].text">
 
Thanks everyone! I will try your suggestions!

ItchyII
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top