Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I select a default value in a dynamicaly populated dropdown (<select>)?

Dynamic Population

How do I select a default value in a dynamicaly populated dropdown (<select>)?

by  TruthInSatire  Posted    (Edited  )
[color gray]<!--- this query will get the data to populate the select box--->[/color]
<cfquery datasource="#datasource#" name="qSelectData">
SELECT Item, Value
FROM ItemTable
ORDER BY Item
</cfquery>


[color gray]<!--- Create a variable to use as the value you want to select. this could be the value of a seperate query, session variable, form variable, or anything you wish to compair. --->[/color]
<cfset defaultValue = "mySelectedItem">
<select name="ItemMenu">
<option value="">Choose Item</option>
<cfoutput query="qSelectData">

[color gray]<!--- Inside the option tag check to see
if the value of the loop is the value
you want to display. --->[/color]
<option value="#Value#"
[color red]<cfif qSelectData.Value eq defaultValue>
Selected
</cfif>[/color]
>#Item#</option>
</cfoutput>
</select>


If the values in the <cfif>...</cfif> match the produced output will look something like:
<option value = "mySelectedItem" selected>item</option>
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top