This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

How to dynamically set the selected item in a drop-down list?

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my UpdateProfile.jsp page, I use a JavaBean to retrieve customer info from the database.
I want the UpdateProfile page to display the information that the customer has originally entered the time he registered or in his last profile update. For example, something like this:
First name: John
Last Name: Doe
Street: 111 2nd Street N
Apartment: 345
City: Denver
State: Colorado
Zip: 98765
It's basically easy, but I got stuck at the State item, which is a drop down list like the following:
<select name=state>
<option value="Alabama" id="Alabama" selected>Alabama
<option value="Alaska" id="Alaska">Alaska
<option value="Arizona" id="Arizona">Arizona
<option value="Arkansas" id="Arkansas">Arkansas
<option value="California" id="California">California
<option value="Colorado" id="Colorado">Colorado
<option value="Connecticut" id="Connecticut">Connecticut
<option value="Delaware" id="Delaware">Delaware
... ...
</select>
Take the above for example, I can certainly retrieve the value of the State, Colorado, but then how do I make it the selected item in the list? This has to be done dynamically. See what I want? Any idea?
 
Sheriff
Posts: 67651
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When your JSP is generating the options, simply place a scriplet which tacks the "selected" attribute on the option that matches the appropriate state (Colorad in your example) rather than hard-coding it on Alabama.
If your options are being automatically generated from a list of states (as opposed to being hard-coded in the JSP) this is even easier.
What aspect of this has you stumped?
hth,
bear
 
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have another suggestion.
whenever i have to fill data in a jsp with dynamic content i call a javascript funtion onload which then fill's up the data for me.
for eg taking ur page as an example

i will have a function say
function onload_filldata(){
document.FORM_NAME.firstname.value = '<%=oBean.getFirstName()%>';
document.FORM_NAME.lastname.value = '<%=oBean.getLastName()%>';
.
.
.
.
//and similarly for the state
document.FORM_NAME.state.value = '<%=oBean.getState()%>';

}
and i will call this function onload of the body.
i think by doin this i am able to seperate all my scriplet tags into a single location and debugging for me will be easy at the same time say for example in ur case where u have to select a option in a select it avoids nested loop.
this is just a suggestion though ..!!!
correct me if u find anything wrong.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
U can use the following code snippet.
<CODE>
for(i=0;i<document.form.state.length;i++)
{
if (document.form.state.value == "<%stateValue%>")
{
document.form.state.selectedIndex = i;
}
}
</CODE>
where stateValue variable inside the sripplet tag is a jsp varaible which wud hold the value of the state to which the combo has to be high lighted to.
Regards
Geeta
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList sL = // get all the states in this.
String selState = "Colarado";
<select name="states">
<%
for(int i=0;i<sL.size();i++)
{
if (selState.equals(sL.get(ac)))
{ %>
<option value="<%=(String)sL.get(i)%>"SELECTED><%=(String)sL.get(i)%>
<% }
else
{ %>
<option value="<%=sL.get(i)%>"><%=sL.get(ac)%>
<% } %>
</option>
<%} %>
</select>
 
reply
    Bookmark Topic Watch Topic
  • New Topic