?
Solved

need to show United states as the first value in drop down

Posted on 2007-11-16
5
Medium Priority
?
368 Views
Last Modified: 2008-05-24
Hi Guys

I have a drop down menu which shows all the countries from a dataBean. What I need is to make the United States at the top. Could anyone please help me on how I can acheive this in the most efficient way. Any help will be greatly appreciated.

here is my code

<td id="<c:out value="WC_${prefix}_AddressEntryForm_TableCell_20"/>">
            
            <select class="logon select" id="<c:out value="WC_AddressEntryForm_FormInput_${prefix}country_1"/>" name="<c:out value="${prefix}country"/>" onchange="javascript:loadStatesUI(this.options[this.selectedIndex].value,<c:out value="${formName}"/>, '<c:out value="${prefix}"/>')" >
                  <c:forEach var="country" items="${countryBean.countries}">
                        <option value="<c:out value="${country.code}"/>"
                              <c:if test="${country.code eq paramSource.country || country.displayName eq paramSource.country}">
                                    selected="selected"
                              </c:if>
                        ><c:out value="${country.displayName}"/></option>
                  </c:forEach>
            </select>
      </td>
0
Comment
Question by:huzefaq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 17

Assisted Solution

by:contactkarthi
contactkarthi earned 400 total points
ID: 20299747
iterate the list first and see if unitedstates is there in the list if it is there add it. then add all the other countries.

or you can manipulate the bean at serverside and have US as the first vvalue
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 1600 total points
ID: 20299783
huzefaq,

I am not a Java or Jscript expert but the most efficient way is to have option use the selected attribute.  It won't matter where in the list it appears it will be selected (or show in the select list) by default.  Of course that may not be something you wish to allow (US selected as default) but is a common way to do what you want.

To do this you would test the values you write as you loop.  If the value is US then you add the 'selected="selected"' attribute.  For example the resulting html would be  ...

<option value="US" selected="selected">US</option>

Let me know if you have any questions or need more information.

b0lsc0tt
0
 

Author Comment

by:huzefaq
ID: 20300000
Thanks Guys

I actually tried adding the code  between teh options but it gives an error. Would you guys know anything about this error
   <c:if test="${country.code == "USA"}">
            selected="selected"
    </c:if>

ERROR
----------------
Unable to locate tag attribute info for tag attribute USA

ENTIRE SELECT
---------------------
<select class="logon select" id="<c:out value="WC_AddressEntryForm_FormInput_${prefix}country_1"/>" name="<c:out value="${prefix}country"/>" onchange="javascript:loadStatesUI(this.options[this.selectedIndex].value,<c:out value="${formName}"/>, '<c:out value="${prefix}"/>')" >
                  <c:forEach var="country" items="${countryBean.countries}">
                        <option value="<c:out value="${country.code}"/>"
                              <c:if test="${country.code == "USA"}">
                                    selected="selected"
                              </c:if>
                              <c:if test="${country.code eq paramSource.country || country.displayName eq paramSource.country}">
                                    selected="selected"
                              </c:if>
                        ><c:out value="${country.displayName}"/></option>
                  </c:forEach>
            </select>
------------------------------------------
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 1600 total points
ID: 20300688
Remember Jscript is new to me but do you need to escape the double quotes or use other delimiters for USA?

<c:if test="${country.code == "USA"}">

In many languages the use of the double quote in front of USA would end the one that started after test=.  A JScript expert will probably be more help on this though.  I just don't know the specifics of the language to say for sure. :)

Let me know how that works or if you have a question.  One comment, you might want to add a test to make sure there is no conflict between the new code to select USA and the next part that preselects an option based on "paramSource.country."

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 21640695
I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question