We help IT Professionals succeed at work.

how to show a specific value in an option

royalcyber
royalcyber asked
on
Medium Priority
206 Views
Last Modified: 2010-04-01
Hi I am using an option in order to help the user to select a particular country. What I need is that if a user needs to edit a specific record the option should show the country name which the user had entered before

Here is my code

<td>
                  <select name="COUNTRY" class="textfield">
                  
            <option value='0' selected> 'Please select one'</option>
<%
                        for(int key_index = 0; key_index < country_keys.length; key_index++) {
                              String next_key   = (String)country_keys[key_index];
                              String next_value = (String)countries.get(next_key);
                              String selected_country = "";
                              if(selected_event.getCountry() == (null) || selected_event.getCountry() == "") { selected_country = "selected"; }
                              else                                                   { selected_country = "";}
%>
                              <option value="<%= next_key %>" <%= selected_country %>><%= next_value %></option>
<%                        } %>
                  </select>
            </td>

In this example if there is a value for selected_event.getCountry than the option should show that value, in other words it should be selected. Right now it shows the last country by default

Any help will be greatly appreciated
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2004

Commented:
As a pure guess:

    <td>
       <select name="COUNTRY" class="textfield">
               
          <option value='0' selected> 'Please select one'</option>
<%
                    for( int key_index = 0; key_index < country_keys.length ; key_index++ )  
                    {
                         String next_key   = (String)country_keys[key_index];
                         String next_value = (String)countries.get(next_key);
                         String selected_country = "";
                         if( selected_event.getCountry() != (null) && selected_event.getCountry().equals( next_value ) )
                         {
                             selected_country = "selected";
                         }
                         else                                                  
                         {
                             selected_country = "";
                         }
%>
                         <option value="<%= next_key %>" <%= selected_country %>><%= next_value %></option>
<%                
                    }
%>
               </select>
          </td>
CERTIFIED EXPERT
Top Expert 2004

Commented:
it was this line:

                         if( selected_event.getCountry() != (null) && selected_event.getCountry().equals( next_value ) )

that I changed...

It probably should be:

                         if( selected_event.getCountry() != (null) && selected_event.getCountry().equals( next_key ) )

But you don't say exactly what selected_event is...

Tim

Author

Commented:
I have actually stored the countries in a LinkedHashMap
 
LinkedHashMap countries = (LinkedHashMap)request.getAttribute("COUNTRIES");

next_key = key of hashmap
next_value = value of hashmap

Sorry it was a typo the real code was like this

    <td>
       <select name="COUNTRY" class="textfield">
               
          <option value='0' selected> 'Please select one'</option>
<%
                    for( int key_index = 0; key_index < country_keys.length ; key_index++ )  
                    {
                         String next_key   = (String)country_keys[key_index];
                         String next_value = (String)countries.get(next_key);
                         String selected_country = "";
                         if( selected_event.getCountry() == (null) && selected_event.getCountry().equals("") )
                         {
                             selected_country = "";
                         }
                         else                                                  
                         {
                             selected_country = "selected";
                         }
%>
                         <option value="<%= next_key %>" <%= selected_country %>><%= next_value %></option>
<%                
                    }
%>
               </select>
          </td>
Siva Prasanna KumarPrincipal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
                      if( selected_event.getCountry() == (null) || selected_event.getCountry().equals("") )
basically change the above if satement as foliows & where are you setting selected_event????
CERTIFIED EXPERT
Top Expert 2004
Commented:
As I said, shouldn't:

                         if( selected_event.getCountry() == (null) && selected_event.getCountry().equals("") )
                         {
                             selected_country = "";
                         }
                         else                                                  
                         {
                             selected_country = "selected";
                         }

be:

                         if( selected_event.getCountry() != null && selected_event.getCountry().equals( next_value ) )
                         {
                             selected_country = "selected";
                         }
                         else                                                  
                         {
                             selected_country = "";
                         }

?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
ManishLead
CERTIFIED EXPERT
Commented:
<option value='0'> 'Please select one'</option>
<%
                    for( int key_index = 0; key_index < country_keys.length ; key_index++ )  
                    {
                         String next_key   = (String)country_keys[key_index];
                         String next_value = (String)countries.get(next_key);
                         String selected_country = "";
                         if( selected_event.getCountry() != (null) && selected_event.getCountry().equals( next_value ) )
                         { %>
  <option value="<%= next_key %>" selected><%= next_value %></option>

                <%  } %>
                         <option value="<%= next_key %>"><%= next_value %></option>
<%                
                    }
%>
               </select>



karan

CERTIFIED EXPERT
Top Expert 2004

Commented:
that's what I said with the code shuffled a bit... :-/
ManishLead
CERTIFIED EXPERT

Commented:
sorry..

%  } else %>
           <option value="<%= next_key %>"><%= next_value %></option>
<%                
              }
%>
Ya tim , u r right..!

Author

Commented:
Thank you very much guys
CERTIFIED EXPERT
Top Expert 2004

Commented:
Glad to be of help :-)

Good luck!

Tim
ManishLead
CERTIFIED EXPERT

Commented:
:)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.