Skip to main content
Inspiring
February 1, 2013
Answered

Dynamically setting an option as selected in cfselect

  • February 1, 2013
  • 1 reply
  • 1138 views

Hi,

I have a cfselect that is defined as such:

 

<cfselect name="ScheduledFollowup" >

<option value=""></option>

<cfloop index="fol" from="0" to="50">

  <option value="<cfoutput>#fol#</cfoutput>>

   <cfoutput>#fol#</cfoutput>

  </option>

</cfloop>

</cfselect>

What I need to do is insert a "selected" attribute on the option that matches a value retrieved in an earlier query.  I have tried imbedding a <cfif> statement but that doesn't work.

Any ideas?

This topic has been closed for replies.
Correct answer NatheManning

I have tried this without success:

<tr id="ScheduledFollowupRow">

  <td valign="top">Scheduled Follow Up<br/>(Number of Months)</td>

  <td valign="top">

<cfset Selected = "">

<cfselect name="ScheduledFollowup" >

  <option value=""></option>

  <cfloop index="fol" from="0" to="50">

   <cfif fol EQ #SelectVisit.FollowupMonths#>

    <cfset Selected = " selected">

   </cfif>

   <option value="<cfoutput>#fol# #Selected#</cfoutput>">

    <cfoutput>#fol#</cfoutput>

   </option>

  </cfloop>

</cfselect>

  </td>

</tr>

What am I doing wrong?


OK. This code worked.

  <td valign="top">

<cfselect name="ScheduledFollowup" selected="#SelectVisit.FollowUpMonths#">

  <option value=""></option>

  <cfloop index="fol" from="0" to="50">

   <cfif fol EQ SelectVisit.FollowupMonths>

    <option value="<cfoutput>#fol#</cfoutput>" selected >

   <cfelse>

    <option value="<cfoutput>#fol#</cfoutput>" >

   </cfif>

    <cfoutput>#fol#</cfoutput>

   </option>

  </cfloop>

</cfselect>

  </td>

1 reply

Inspiring
February 1, 2013

<cfselect> has a selected attribute.  It always works when  I use it.

Inspiring
February 1, 2013

I tried that. The documentation states that it only works when the cfselect is populated from a query.

WolfShade
Legend
February 1, 2013

You could always fake a query for the #fol#.

<cfscript>

tmp = QueryNew("field1","{datatype}");

tmp2 = QueryAddRows(tmp,x); <!--- x being the number of rows --->

tmp3 = QuerySetCell(tmp,"field1","{value}",1);

tmp3 = QuerySetCell(tmp,"field1","{value}",2);

...

</cfscript>

<cfdump var="#tmp#">

<cfoutput query="tmp">

#field1#<br />

</cfoutput>

^_^