We help IT Professionals succeed at work.

Start jquery dynamic selects with default values

Lee R Liddick Jr
on
Medium Priority
474 Views
Last Modified: 2012-08-14
I'm using a bind to dynamically populate a drop down select box but I want the first values in the box that the user is presented with to be null/default.

Now, when the page loads, the first option appears in the 1st select so that automatically populates the 2nd select.  I want the first select to be null/default to no value.  I have tried a couple things and either the jquery doesn't work or I just get blank boxes.

<!--- my cfc --->
    <!--- Get array of facilities --->
    <cffunction name="getFacility" access="remote" returnType="array">
        <!--- Define variables --->
        <cfset var data="">
        <cfset var result=ArrayNew(2)>
        <cfset var i=0>

		<!--- Facility Query --->
        <CFQUERY NAME="data">
          select	a.fac_id, a.fac_nm 
          from	tbl_FAC a
          where	a.fac_id = #session.fac_id#
          </cfif>
        </CFQUERY>

        <!--- Convert results to array --->
        <cfloop index="i" from="1" to="#data.RecordCount#">
            <cfset result[i][1]=data.fac_id[i]>
            <cfset result[i][2]=data.fac_nm[i]>
        </cfloop>

        <!--- And return it --->
        <cfreturn result>
    </cffunction>

    <!--- Get surveys by facility --->
    <cffunction name="getSurvey" access="remote" returnType="array">
        <cfargument name="fac_id" type="numeric" required="no" default="9999">

        <!--- Define variables --->
        <cfset var data="">
        <cfset var result=ArrayNew(2)>
        <cfset var i=0>

<!--- Survey Query --->
<CFQUERY NAME="data">
  select	a.svy_id, a.svy_nm, a.hcf_id 
  from	tbl_survey a
  where	a.fac_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.fac_id#">
</CFQUERY>

        <!--- Convert results to array --->
        <cfloop index="i" from="1" to="#data.RecordCount#">
            <cfset result[i][1]=data.svy_id[i]>
            <cfset result[i][2]=data.svy_nm[i]>
        </cfloop>

        <!--- And return it --->
        <cfreturn result>
    </cffunction>

<!--- here is my form on page1.cfm --->
<cfform>
<table>
    <tr>
        <td>Select Facility:</td>
        <td><cfselect name="fac_id"
                bind="cfc:myCFC.getFacility()"
                bindonload="true" /></td>
        <td>Select Survey:</td>
        <td><cfselect name="svy_id"
                bind="cfc:myCFC.getSurvey({fac_id})" /></td>
    </tr>
	<tr>
    	<td colspan="4" style="float:right;">
        	<cfinput name="varAction" CLASS="form_button_050" TYPE="button" VALUE="Run">
        </td>
    </tr>
</table>
</cfform>

Open in new window


So, how can I get the facility select to show 'Select Facility' and of course the survey select to be blank (at least until they select the facility)?
Comment
Watch Question

Replace the loop code in your getFacility function with the below code

    <cfset result[1][1] = "0" />
    <cfset result[1][2] = "--- Select Facililty ---" />
 <!--- Convert results to array --->
        <cfloop index="i" from="1" to="#data.RecordCount#">
            <cfset result[i+1][1]=data.fac_id[i]>
            <cfset result[i+1][2]=data.fac_nm[i]>
        </cfloop>

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.