Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65658
129
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
 
Devesh H Rao
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Geeta Ramasami
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • 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
 
Raghunandhan Ramanujam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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>
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!