How do I get the content (not the name) of an option in a choice box?
Created May 8, 2012
Alessandro A. Garbagnati, Jun 19, 2001However, if you want to get clever, read on ... -Alex]
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> </select>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.
Hello J Allen,
<input type="hidden" name="ddtext" value="">
<form>
<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>
</select>
</select>
</form>
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.
Sincerely,
Thomas Dietrich