Copy link to clipboard
Copied
Is there a way of making a field visible and Required if a particular selection is made in a drop down menu.
For instance I have 4 different options in my drop down menu including "Other" and if Other is selected, I want a new text field to be visible so the user can add more specific information.
Any help or guidance would be appreciated.
If you're using ColdFusion attribute required, I'm not sure how to change that dynamically. But if you're doing client side validtion with javascript - or double checking it after submitting with ColdFusion, this is easy in principle.
<script language="Javascript">
function checkifother()
{
myselectfield = document.getElementById('myselect');
explanationdiv = document.getElementById('explanationdiv');
if ( myselectfield.va
...Copy link to clipboard
Copied
Done with a lot of JavaScript. Basically, you set that text field to "display:none;" using CSS so that it's hidden when the page loads.
Then, using either a function or inline JavaScript in the "onchange" event of the select, check the value (or text) of the selected option. If it's "other", then
document.getElementById("hiddenFieldName").style.display = 'inline'
..and set the form validation to require it if the display is not "none".
^_^
Copy link to clipboard
Copied
If you're using ColdFusion attribute required, I'm not sure how to change that dynamically. But if you're doing client side validtion with javascript - or double checking it after submitting with ColdFusion, this is easy in principle.
<script language="Javascript">
function checkifother()
{
myselectfield = document.getElementById('myselect');
explanationdiv = document.getElementById('explanationdiv');
if ( myselectfield.value == 'Other')
{explanationdiv.style.display='block'}
else
{explanationdiv.style.display='none'}
}
</script>
<cfform name="myform">
<cfselect name="myselect" id="myselect" onchange="Javascript:checkifother();">
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Other">Other</option>
<option value="Option 4">Option 4</option>
</cfselect>
<div id="explanationdiv" style="display:none">
<cfinput type="text" name="otherexplanation">
</div>
</cfform>
Copy link to clipboard
Copied
Thanks, did the job nicely.