Hide Form Field based on Another Form Selection

Lee R Liddick Jr
Lee R Liddick Jr used Ask the Experts™
on
I have a form that has various fields.  In particular, if a user selects a specific value on one field, then I want another field to appear.

So if user selects 'Yes' to field 1, then I want field 2 to appear.  If user selects, 'No' to field 1, then I want to keep field 2 invisible to the user.
<!--- Dashboard Changes Required Options --->
<CFSET d = 2>
<CFSET selDBChReq = StructNew()>
<CFSET selDBChReq.value[1] = "1">
<CFSET selDBChReq.value[2] = "0">
<CFSET selDBChReq.description[1] = "Yes">
<CFSET selDBChReq.description[2] = "No">
<CFLOOP INDEX="dd" FROM="1" TO="#d#">
   <CFIF selDBChReq.value[dd] EQ varDBChReq>
      <CFSET selDBChReq.tag[dd] = " SELECTED">
   <CFELSE>
      <CFSET selDBChReq.tag[dd] = "">
   </CFIF>
</CFLOOP>

<FORM NAME="frmEditData" ACTION="edit_data.cfm" METHOD="post">
<table>
<TR>
	<TD CLASS="form_label" ALIGN="left" NOWRAP>Field 1</TD>
    <TD ALIGN="left">
       <SELECT NAME="edtDBChReq" CLASS="form_combo_normal" STYLE="width:124px;">
       <CFLOOP INDEX="dd" FROM="1" TO="#d#">
		<CFIF #selDBChReq.description[dd]# EQ #qryEDT.strDash#>
          <CFOUTPUT>
          <OPTION VALUE="#selDBChReq.value[dd]#"#selDBChReq.tag[dd]# selected>#selDBChReq.description[dd]#</OPTION></CFOUTPUT>
		<CFELSE>
          <CFOUTPUT>
          <OPTION VALUE="#selDBChReq.value[dd]#"#selDBChReq.tag[dd]#>#selDBChReq.description[dd]#</OPTION></CFOUTPUT>        
        </CFIF>
       </CFLOOP>
       <CFOUTPUT>
       </SELECT>
    </TD></CFOUTPUT>
</TR>
<!!----this part needs hidden unless 'yes' is selected above ----->
<TR>
 <TD CLASS="form_label" ALIGN="left">Field 2</TD>
 <TD ALIGN="left"><SELECT NAME="edtDBChTyp" CLASS="form_combo_normal" STYLE="width:124px;">
 <CFLOOP QUERY="qryDBChTyp">
    <CFIF #qryDBChTyp.br_dashchangetype_id# EQ #qryEDT.intBR_DashChangeType_ID#>
       <CFOUTPUT>
       <OPTION VALUE="#qryDBChTyp.br_dashchangetype_id#" SELECTED>#qryDBChTyp.br_dashchangetype_description#</OPTION></CFOUTPUT>
    <CFELSE>
       <CFOUTPUT>
       <OPTION VALUE="#qryDBChTyp.br_dashchangetype_id#">#qryDBChTyp.br_dashchangetype_description#</OPTION></CFOUTPUT>
    </CFIF>
 </CFLOOP>
 <CFOUTPUT>
 </SELECT>
 </TD></CFOUTPUT>
</TR>
</table>
</form>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2015
Commented:
Try a bit of jquery

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
   toggleRow($('#yourList').val());
   $('#yourList').change(function(){
      toggleRow($(this).val());
   });
 });
 function toggleRow(value) {
    if (value == "1") // 1 == yes
       $('#yourListRow').show();
    else
       $('#yourListRow').hide();
 }
</script>
</head>
<body>
<FORM NAME="frmEditData" ACTION="edit_data.cfm" METHOD="post">
<table>
<TR>
      <TD CLASS="form_label" ALIGN="left" NOWRAP>Field 1</TD>
    <TD ALIGN="left">
       <SELECT id="yourList" NAME="edtDBChReq" CLASS="form_combo_normal" STYLE="width:124px;">
                  <OPTION value="1">Yes</OPTION>
                  <OPTION value="0">No</OPTION>
       </SELECT>
    </TD>
</TR>
<!!----this part needs hidden unless 'yes' is selected above ----->
<TR id="yourListRow">
 <TD CLASS="form_label" ALIGN="left">Field 2</TD>
 <TD ALIGN="left">
      <SELECT NAME="edtDBChTyp" CLASS="form_combo_normal" STYLE="width:124px;">
            <OPTION value="1">some option 1</OPTION>
       </SELECT>
 </TD>
</TR>
</table>
</form>
</body>
</html>
Zvonko זְאֵבSystems architect
Top Expert 2006
Commented:
My proposal needs no jQuery.

Add an onChange event handler to your select:
<SELECT NAME="edtDBChReq" CLASS="form_combo_normal" STYLE="width:124px;" onChange="ShowHideSel(this)" >

Open in new window

Define this two functions anywhere on the page but best at the top of page:
<script>
function ShowHideSel(theSel){
  $("idSeconField").style.display=(theSel.value=="1")?"":"none";
}
function $(theID){
  return document.getElementById(theID);
}
</script>

Open in new window

And give your <tr> the used ID from upper function:
<TR id="idSeconField">

Open in new window

OK, now check it.
Most Valuable Expert 2015

Commented:
Don't forget to also call the function when the page loads to set the row visibility,  like the jquery example does.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Zvonko זְאֵבSystems architect
Top Expert 2006
Commented:
If you want that initial setting to addapt to page load time then for that is this my trick:

<SELECT NAME="edtDBChReq" CLASS="form_combo_normal" STYLE="width:124px;" onChange="ShowHideSel(this)" > 
<script>document.forms[0].edtDBChReq.onchange();</script>

Open in new window

Zvonko זְאֵבSystems architect
Top Expert 2006

Commented:
Of course the fine art is to put the <script> AFTER the </select>

I posted it together to make it better visible.

Zvonko זְאֵבSystems architect
Top Expert 2006

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial