Solved

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

Posted on 2007-11-16
5
348 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
  • 3
5 Comments
 
LVL 17

Assisted Solution

by:contactkarthi
contactkarthi earned 100 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 400 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 400 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to implement Singleton Design Pattern in Java.
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 …

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now