Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting value and innerText of Select box in servlet

 
Ajit Kanada
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have a jsp page.It contains select box
<select name="sbox">
<option value="101">text1</option>
<option value="101">text1</option>
</select>
Question is..
How do i get both value and text of a option element in Servlet
Which method of request object ll do the job?
In servlet if i say
request.getParameterValues("sbox").length
Its length is one ..
Can anyone help?
Ajit
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The client will return name=value as a key/value pair to your Servlet. The name of your select box is sbox, the value will be 101.
Since the both values are 101, the value returned to your servlet will always be 101.
How do i get both value and text of a option element in Servlet
The client only sends the name/value pair. It doesn't send the other text. Therefore the data won't be available to you unless you do something to get it yourself.
You might find that the text is already available in the Servlet from when the page was sent to the client. Otherwise you can set up some JavaScript on the client to send the select text as an extra parameter.
Dave
 
Ajit Kanada
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I got the solution to get both value and innerText of select box
i take on hidden fied say with id="txt"
in script
function getForumName(selbox)
{

txt.value = selbox.all[selbox.selectedIndex].innerText.substring()
}

now this gives me selected options text
and request.getParameter("sbox") gives me value of option

cheers
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why bother with innerText when the text attribute of the option contains the string you want?
hth,
bear
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic