Passing text contained between <option> tags of a select list

Hello All,

I'm using jsp on an html form, I need to obtain the text betweent the <option> </option> tags of a (jstl created) select drop down list. Unfortunately each option is created dynamically using a Java Bean populated ArrayList (or some similar container), and as usual the value is a numberical ID related to the option, which I don't want to make use of.

Let's assume I can't make use of that container, and that I have no way of querying on that numerical ID to get the associated string.

I need to make use of the text between the tags, so I want to see if there is some way I can assign the text of the SELECTED option to some hidden variable for me to work with (within my javascript function.) So far the action of my select input is the call of my javascript function

<select name="catGroupId" id="search_in" >

<c:forEach var="topCategory" items="${catalog.topCategories}" varStatus="counter">

{some logic}

<option value="<c:out value="${topCategory.categoryId}" escapeXml="false"/>"><c:out value="${topCategory.description.name}" escapeXml="false"/></option>

{/some logic}

</c:forEach}

Thanks in advance for anyone who reads this or can give me some insight on this.

Thanks you.....Feel free to ask me for clarification if needed.

rza>

[1389 byte] By [rzawrecktaha] at [2007-10-2 17:36:06]
# 1

The first option would be to get rid of the id as a value if it is useless. Instead use the value you really care about:

<option value="<c:out value="${topCategory.description.name}" escapeXml="false"/>">

<c:out value="${topCategory.description.name}" escapeXml="false"/>

</option>

Another option, if the id is usefull sometimes, just not now, would be to create an option value that contains both the id and the text of interest, deliminated by a ;

<option value="<c:out value="${topCategory.categoryId}" escapeXml="false"/>;<c:out value="${topCategory.description.name}" escapeXml="false"/>">

<c:out value="${topCategory.description.name}" escapeXml="false"/>

</option>

So then, in the servlet that processes the page, you can use the getParameterValues to get all the values. 0 and even indexed values are the ids, and odd indexed ids are the text:

String[] values = getParameterValues("catGroupId");

Map<String,String> catGroups = new HashMap<String,String>(values.lenght/2);

for (int groupNum = 0; groupNum < values.length; groupNum+=2) {

catGroups.add(values[groupNum], values[groupNum+1]);

}

So then you have a map of all the selected ids to values... as an example

stevejlukea at 2007-7-13 18:53:23 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for your help...

Actually my coworker helped me w/ a much easier way. I appended the .name string to the option value, so when selected, in my javascript function I parse out what I need.

<option value="<c:out value="${topCategory.categoryId}${topCategory.description.name}" escapeXml="false"/>;<c:out value="${topCategory.description.name}" escapeXml="false"/>">

<c:out value="${topCategory.description.name}" escapeXml="false"/>

</option>

The new problem is that in my javascript function, I need to reassign a javascript string/var to the html form element's value.

function blah(form){

var temp = "";

var temp1 = "";

var temp2 = "";

temp = form.catGroupId.value;

temp2 = temp.substring(0,5);

temp1 = temp.substring(5);

form.catGroupId.value = temp2; < NOT WORKING

}

Thanks again. I've tried using <%= => around temp2, and it won't work, i don't know if its in the way I'm assigning it. but I am clueless for now.

Raza

rzawrecktaha at 2007-7-13 18:53:23 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...