How do I get the content (not the name) of an option in a choice box?

thomas dietrich

[ The cleanest solution is to build the HTML yourself, and make the "value" attribute something you know, like the database id, or even the content itself. From getParameter() returns a value of a choice box, I ...:

Alessandro A. Garbagnati, Jun 19, 2001
Hi, If I've understood your problem, you have somthing like this:
<select name=whatever>
<option value=value_1>text_1</option>
<option value=value_2>text_1</option>
<option value=value_3>text_3</option>
The request.getParameter("whatever") it's returning you the first value selected (or the only one in a case of a single selection), and there is no way, from the request object, to get the text. The reason it's simple. That information it's not part of the request when you submit the form. The only information passed it's the value.

There are two solutions. One is to maintain the array (or map or whatever you use for generating the dropdown box) between responses so, when you get the request you read the value and you get back your text from the array (or map or etc.). The secon solution is to change the value of the option to include the text, maybe with some kind of separator (could use |), and when you get the value, you just split in the value and the text.

Personally I consider cleaner the first solution.

However, if you want to get clever, read on ... -Alex]

Hello J Allen,

<select name="dd" onChange="document.forms[0].ddtext.value = document.forms[0].dd[document.forms[0].dd.selectedIndex].text;">
<option value="1">One</option>
<option value="2">Two</option>

<input type="hidden" name="ddtext" value="">

This is a working example of cheesiness at it's finest. :) the fields are fully qualified so that you can see what they look like before you start taking short cuts.


Thomas Dietrich

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.