ColdFusion Function: Display a US States pulldown menu
September 29th, 2008
Here is a CF Function that generates a pulldown of USA states:
<cffunction name="SelectState">
<cfargument name="selected" default="Select State:">
<cfargument name="name" default="state">
<cfargument name="id" default="#name#">
<cfargument name="omit" default="">
<cfargument name="ability" default="enabled">
<cfargument name="onchange" default="">
<cfargument name="value" default="abbreviations">
<cfargument name="label" default="fullnames">
<cfset abbreviations=
"00,AL,AK,AZ,AR,CA,CO,CT,DE,DC,FL,GA,HI,ID,IL,IN,IA,KS,KY,LA,ME,MD,MA,MI,MN,MS,MO,MT,NE,NV,NH,NJ,NM,NY,NC,ND,OH,OK,OR,PA,PR,RI,SC,SD,TN,TX,UT,VT,VA,WA,WV,WI,WY,--">
<cfset fullnames=
"Select State:,Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware,D.C.,Florida,Georgia,Hawaii,Idaho,Illinois,Indiana,Iowa,Kansas,Kentucky,Louisiana,Maine,Maryland,Massachusetts,Michigan,Minnesota,Mississippi,Missouri,Montana,Nebraska,Nevada,New Hampshire,New Jersey,New Mexico,New York,North Carolina,North Dakota,Ohio,Oklahoma,Oregon,Pennsylvania,Puerto Rico,Rhode Island,South Carolina,South Dakota,Tennessee,Texas,Utah,Vermont,Virginia,Washington,West Virginia,Wisconsin,Wyoming,NOT IN USA">
<cfoutput>
<select
name="#name#" id="#id#" #ability#
<cfif isdefined("style")>style="#style#"</cfif>
<cfif isdefined("onchange")>onchange="#onchange#"</cfif>
>
<cfloop from="1" to="#listlen(fullnames)#" index="state">
<cfif not listcontains(omit,#listgetat(evaluate(value), state)#) and not listcontains(omit,#listgetat(evaluate(label), state)#)>
<option value="#listgetat(evaluate(value), state)#"
<cfif #listgetat(evaluate(value), state)# is "#selected#">selected</cfif>
<cfif isdefined("style")>style="#style#"</cfif>>
#listgetat(evaluate(label), state)#
</option>
</cfif>
</cfloop>
</select>
</cfoutput>
</cffunction>
And to invoke it for display in your form:
#session.utils.SelectState()#
This assumes you loaded the CFC into a session variable called ‘utils’. As you can see, you can pre-select a state, suitable for an update form.
You can configure what is shown for each option and what is the value using the ‘value’ and ‘display’ arguments.
Next week I’ll show a function for displaying countries as well. They work together so that if you choose a state, the country changes to USA. And if you pick another country, the state changes to ‘not in usa’. This is hinted by the ‘onchange’ argument you might have noticed.

I am not a huge fan of using functions for presentation. I think wherever you can get away from it you should. I think a simple solution would be to just return the html needed. Another practice to get away from is trying to stuff a bunch of helper functions into a Util component. Try and abstract related functions into objects. You can see here that there would be some related form methods here. I also think you should get away from loading this "Util" component into session scope. Unless you need to store data that is related to the session this component would be a singleton and there for created only 1x.
Just my 2 cents
Hi Dan, this was used for an admin – updating company and user records and such. Lots of use. But I understand your recommendation. Thanks for the post!