Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-11-16
5
Medium Priority
?
372 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

618 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