Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColdFusion auto select option

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!

like image 228
Alex Avatar asked Apr 12 '26 09:04

Alex


1 Answers

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#" />
like image 66
Adam Tuttle Avatar answered Apr 15 '26 02:04

Adam Tuttle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!