What is the best way to automatically set a selected item for a select/option element on post-back? Here's the way we're currently doing it:
<select id="grade" name="grade">
<option value="A"<cfif form.grade = 'A'> selected="selected"</cfif>>A</option>
<option value="B"<cfif form.grade = 'B'> selected="selected"</cfif>>B</option>
<option value="C"<cfif form.grade = 'C'> selected="selected"</cfif>>C</option>
<option value="D"<cfif form.grade = 'D'> selected="selected"</cfif>>D</option>
<option value="F"<cfif form.grade = 'F'> selected="selected"</cfif>>F</option>
</select>
Is there a cleaner or easier way to do this with ColdFusion? Thanks in advance!
In my opinion, one of the best ways to go is to use a CFSelect:
<cfquery name="getGrades" datasource="#application.dsn#">
select gradeLetter from Grades
</cfquery>
<cfselect
name="grade"
query="getGrades"
display="gradeLetter"
value="gradeLetter"
selected="#form.grade#" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With