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

how i get all options value from select tag

Status
Not open for further replies.

fuadhamidov

Programmer
Sep 22, 2003
98
TR
hi
i need to get all options value from select tag. i have some code like
<select name=TEST size=5>
<option value='...'>large
<option value='...'>small
...
</select>
is there any method to get all options value. ot smth like that
 
Hi,

You need to use javascript to send all values of a select tag to the server. On the server you can use request.getParameterValues() which will return the array of strings, which has all the values of the select tag.

Cheers
-Venu
 
Why do you need such a thing by the way?

Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
turkey_clr.gif
 
because i fill select tag by using js. and i want all option that i have filled in it.
now OK. i can read request.getParameterValues(&quot;< select.name >&quot;);
thanks
 
hi
i have some problem with request.getParameterValues(). it works a while (or i didn't see ita error ) and after i have changed some code i see it gets only selected value.
i don't know where is my mistake
my code

menu.jsp
<SELECT NAME=&quot;SIZE2&quot; size='5'>
</SELECT></TD></TR>
<button name=&quot;SUBMIT&quot; type=button onclick='insert()' value='Insert'>Insert<button>

//function 'insert() -> insert options

target.jsp
String size2[] = request.getParameterValues( &quot;SIZE2&quot; );
System.out.println( size2.length );
System.out.println( size2[0] );
// prints 1 and selected option's value but i need all options' value

 
>>> System.out.println( size2[0] );

This is just printing the first element of the size2 array - you need to loop the array to display all elements ...
 
yes, i know
>>>System.out.println(size2.length);
prints 1
 
request.getParameterValues( ); only return the selected value. you need separate hidden field to send all option to server.

e.g.
Code:
<select name=TEST size=5> 
  <option value='...'>large
  <option value='...'>small
  ...
</select> 

<!-- replace all options here again with hidden field -->
<input type=hidden name=testOptions value=large/>
<input type=hidden name=testOptions value=small/>
...

Then in the server side.
you can do request.getParameterValues(&quot;testOptions&quot;) to retrieve the hidden field values for the options.
 
Hi,

Try this, If you dont want to use the Java Script follow the byam suggition of storing the values into hidden fields.

Below Example uses the Java Script.

<script language=&quot;javascript&quot;>
function allOptions(){
List = document.forms[0].chosen;
//alert(List.length);
for (i=0;i<List.length;i++)
{
List.options.selected = true;
}
}

</script>
<body>
<form method=&quot;POST&quot; action=&quot;testselect.jsp&quot; onsubmit=&quot;allOptions()&quot;>
<p><select size=&quot;5&quot; name=&quot;chosen&quot; multiple>
<option>test</option>
<option>test1</option>
<option>test2</option>
<option>test3</option>
<option>test4</option>
</select><input type=&quot;submit&quot; value=&quot;submit&quot; name=&quot;B1&quot; >
</p>
</form>

<%
if(null != request.getParameterValues(&quot;chosen&quot;))
{
String chosenVal[] = request.getParameterValues(&quot;chosen&quot;);
out.println(chosenVal.length);
for(int i=0; i< chosenVal.length; i++)
{
out.println(chosenVal);
}
}
%>

out put >> 5 test test1 test2 test3 test4




Cheers
-Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top