Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setting the selected option in a select element

 
kasi Raju
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Every one,

I have a drop-down box in a JSP, when user selects a value and submits want to display values in the same page.
With the drop down value selected .

when I am trying to this I can display drop-down values and when submitted can get the results but not the drop down values.

Thanks in advance.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64975
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basic HTML: set the selected attribute on the <option> element to be shown as selected.
 
kasi Raju
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply

But how can I do that in JSP page as I am not getting the vales of drop down at all.

After the page submitting getting only results
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64975
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first you need to know which value you want to be selected. I assume you can get that information from the parameters of the page submission.

Then you can use either the <c:if> JSTL tag to decide whether each option needs the selected attribute or not, or you could use the ternary operator of the EL (the ? operator).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64975
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. I have adjusted the topic title to better reflect the content.

P.P.S. This has been discussed before. Search this forum for previous discussions.
 
Ramkumar Subburaj
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more way is, for the option tags, set the id as value.

Use jstl to check if the selected value is available in parameter.

If available, create a script immediately after </select> and do

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64975
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Non-optimum as that would not work if script is disabled. It's best to set the value on the server rather than wait for the client to do it.
 
Ramkumar Subburaj
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
agreed. If javascript is disabled, Most of today's web sites wont work i guess.
Instead of checking whether to set selected or not for each options, imagine there are 100 options, I beleive javascript works better
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64975
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramkumar Subburaj wrote:agreed. If javascript is disabled, Most of today's web sites wont work i guess.

This may be true, but there are still people who disable script thinking it makes them more secure and many shops need to account for this.

Instead of checking whether to set selected or not for each options, imagine there are 100 options, I beleive javascript works better

Disagree. What does the number of options have to do with it in the first place?
 
Ramkumar Subburaj
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the user has selected one option out of 100, in the server side to set the user selected option, you have to check for each option whether it is the one user has selected or not using JSTL and set the selected attribute.

Is there any other way?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64975
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see the problem with checking each option -- especially when the options are usually populated by a loop.

I rarely do anything on the client that can be better done on the server.
 
kasi Raju
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You for the reply

I tried but couldn't get

here is my JSP page


how to retain the value

Thanks
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault, back on June 10, wrote:
Then you can use either the <c:if> JSTL tag to decide whether each option needs the selected attribute or not, or you could use the ternary operator of the EL (the ? operator).


You would put that inside the <option> element at the point where the "selected" attribute would normally go. Presumably you have a way of looking at the "current" object to see whether it should be selected?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic