Skip to main content
February 26, 2009
Question

Set dropdownlist on page load

  • February 26, 2009
  • 1 reply
  • 957 views
Hi! I would like to be able to set 3 dropdownlists on page load. Here's what I have so far:

<cfset form.sFreeState="#state#">
<cfset form.sFreeSubject="#subject#">
<cfset form.sFreeTypeId="#schoolType#">

For some reason, the <select>'s are not set with my values. I am debugging the page, so I can see my variables in the debug section:

Form Fields:
SFREESTATE=Connecticut
SFREESUBJECT=254
SFREETYPEID=17

What am I missing? Thanks!
This topic has been closed for replies.

1 reply

Inspiring
February 26, 2009
To preselect values in a select list, you must pass those value to the <select> list. For example, <cfselect> has a "selected" attribute which is used for this purpose.

Since we cannot see your code, are you actually doing this?

> <cfset form.sFreeTypeId="#schoolType#">

BTW, you do not need either the quotes or # signs in your cfset. Just use

<cfset form.sFreeTypeId = schoolType >
February 27, 2009
Thanks for the response. Yes, in my code I am attempting to set the select fields thusly:

<cfset form.sFreeState="#state#">
<cfset form.sFreeSubject="#subject#">
<cfset form.sFreeTypeId="#schoolType#">

The code is using selects, not cfselects. Do I need to use cfselects to pass a variable to the template to set the displayed value?
Inspiring
February 27, 2009
No, you do not have to use cfselect.

> I am attempting to set the select fields thusly:
> <cfset form.sFreeState="#state#">

That simply changes the value of the #form.sFreeState# variable. It will not pre-select a value in a <select> list. To pre-select an item, you must mark one of the list's options as "selected". Depending on how you generate the list options, you can make the selection more dynamic of course.

<select>
<option value="something" selected>Something</option>
....
</select>

http://www.w3schools.com/TAGS/tag_option.asp