Auto Select Cfselect tag

I currently have a cfselect tag that is populated via a query. I'm trying to get the queried results to automatically select an entry based upon a condition (url.contactid).
QUESTION: How do I get the cfselect tag to automatically select an entry base upon a variable either URL or FORM?

<cfselect name="ContactID" size="25" id="ContactID"> <cfoutput query="contacts">
          <option value="#contacts.ContactID#">#contacts.LastName#, #contacts.FirstName# - #contacts.EmailAddress# ---  #contacts.Company#</option>
        </cfoutput> </cfselect>

Open in new window

Who is Participating?
azadisaryevConnect With a Mentor Commented:
like this:

<cfselect name="ContactID" size="25" id="ContactID">
<cfoutput query="contacts">
  <option value="#contacts.ContactID#" <cfif contacts.contactID EQ SomeValriable>selected="selected"</cfif>>#contacts.LastName#, #contacts.FirstName# - #contacts.EmailAddress# ---  #contacts.Company#</option>

or, if you edited your query and added a new column by concatenating all the column you use in your select's display values, then you could just use 1 line of code to create your cfselect:

<cfselect name="contactID" size="25" QUERY="contacts" DISPLAY="concatenated-qiery-column-name" VALUE="contactID" SELECTED="#SomeVariable#"></cfselect>

jasch2244Author Commented:
Does the "selected" portion of the code refer to a variable as well? example: selected="selected"
no, selected="selected" is just a standard XHTML way to mark a selected option. it is not any cf variable.
you can just use 'selected' if your page's DOCTYPE is not XHTML or if you do not care about your code being XHTML-compliant:

<option value="#contacts.ContactID#" <cfif contacts.contactID EQ SomeValriable>selected</cfif>>...</option>

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.