• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

how to show a specific value in an option

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
0
royalcyber
Asked:
royalcyber
  • 5
  • 3
  • 2
  • +1
2 Solutions
 
TimYatesCommented:
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>
0
 
TimYatesCommented:
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
0
 
royalcyberAuthor 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>
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
                      if( selected_event.getCountry() == (null) || selected_event.getCountry().equals("") )
basically change the above if satement as foliows & where are you setting selected_event????
0
 
TimYatesCommented:
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 = "";
                         }

?
0
 
ManishLeadCommented:
<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

0
 
TimYatesCommented:
that's what I said with the code shuffled a bit... :-/
0
 
ManishLeadCommented:
sorry..

%  } else %>
           <option value="<%= next_key %>"><%= next_value %></option>
<%                
              }
%>
Ya tim , u r right..!
0
 
royalcyberAuthor Commented:
Thank you very much guys
0
 
TimYatesCommented:
Glad to be of help :-)

Good luck!

Tim
0
 
ManishLeadCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now