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

Accessing Text Portion of a Select Option Group

Status
Not open for further replies.

shaunk

Programmer
Aug 20, 2001
402
AU
I have a Select control in a form on my ASP page:

<select name="optPlayer Name" size="1" >
<option selected value = ""> Select the Player </option>
<option value = 1> Bob </option>
<option value = 2> Jeff </option>
<option value = 3> Wayne </option>
<option value = 4> Baz </option>
<option value = 5> Gilbert </option>
</select>

On form submit, it sends the value listed in the 'value=' statement to the server i.e It sends 1, rather than "Bob". Fair enough.
However, is there a way to access the text portion of the select statement as well i.e "Bob".

I want to do this so that I don't need to perform another request to the database to retrieve the Player Name in subsequent pages.

Thanks




The risk with keeping an open mind is having your brains fall out.
Shaunk

 
you will need to use javascript for this (if you want to stay with the HTML you've written today).

why not generate something like this, instead:

Code:
<select name="optPlayer Name" size="1" >
<option selected value = ""> Select the Player </option>
<option value = "1:Bob"> Bob </option>
<option value = "2:Jeff"> Jeff </option>
<option value = "3:Wayne"> Wayne </option>
<option value = "4:Baz"> Baz </option>
<option value = "5:Gilbert"> Gilbert </option>
</select>

Then, you can simply parse the value, splitting the string in two parts, getting the ID and the name in one fell swoop.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
You could change the value from 1 to "Bob" and you'd retirieve "bob", if you need both values, you could make a compound value and separate it in your ASP code part.

something like:

Code:
<select name="optPlayer Name" size="1" >
<option selected value = ""> Select the Player </option>
<option value = "[red]1,bob[/red]"> Bob </option>
...

Then use ASP to split the string at the comma to get the values.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thankyou both. I did think of the compound value option but I think it looks clunky for the users. I have simplified the example somewhat. The "value" portion is actually a 6 digit Player_Id extracted from a table.
cLFlaVA...Can you expand on the Javascript option. I am already using Javascript as a form validator that accesses the element through the 'DOM element ID' attribute.


The risk with keeping an open mind is having your brains fall out.
Shaunk

 
shaunk,

my suggestion for a javascript option (which should be addressed in the javascript forum: forum216) is to have a hidden field in your form. you could then set the value of the hidden field to the textual value of the option.

the hidden field would then be submitted with the form, and you'd be able to access the value that way.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I'm not sure what you mean by : I did think of the compound value option but I think it looks clunky for the users"

The users don't even see the compound value. Unless they actually look at the code, all they'll see is just a normal pull-down.
Its up to you to use that value to obtain the information you want.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Of course. The Users won't see it will they.
Momentary lapse of sanity.
I'll send the compound value to the server and parse it in subsequent pages.
Thanks


The risk with keeping an open mind is having your brains fall out.
Shaunk

 
It's o.k. It happens to the best of us.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top