jturkington
asked on
Enable / Disable form fields with a checkbox
Looking abit of advice on the best way to enable or disable form fields based on whether certain checkboxs have been ticked
For example if i have two checkboxes, I tick "check box one" which in turn enables a text box field and a textarea field. If I uncheck "check box one" the text box and text area are disabled again. The outcome I eventually want is to have 5 or 6 checkboxes enable and disabling a combination of form fields based on whether they are checked or not.
Any example CF code would be much appreciated or better ways of performing this task eg using a multiple select area instead of checkboxs ?
Jonathan
For example if i have two checkboxes, I tick "check box one" which in turn enables a text box field and a textarea field. If I uncheck "check box one" the text box and text area are disabled again. The outcome I eventually want is to have 5 or 6 checkboxes enable and disabling a combination of form fields based on whether they are checked or not.
Any example CF code would be much appreciated or better ways of performing this task eg using a multiple select area instead of checkboxs ?
Jonathan
Also if you want the value entered to be cleared once they are unchecked
then add this
function fncEnable(chknum)
{
if(document.frm["chk"+chkn um].checke d == true)
{
document.frm["txt"+chknum] .disabled = false;
document.frm["txtarea"+chk num].disab led = false;
}
else
{
document.frm["txt"+chknum] .value= '';
document.frm["txtarea"+chk num].value = '';
document.frm["txt"+chknum] .disabled = true;
document.frm["txtarea"+chk num].disab led = true;
}
}
in CF you will have to submit the form everytime to the same page when the checkbox is checked or unchecked and then put cfif conditions around the text box and textarea...
let me know if you want just the CF solution
Regards
Hart
then add this
function fncEnable(chknum)
{
if(document.frm["chk"+chkn
{
document.frm["txt"+chknum]
document.frm["txtarea"+chk
}
else
{
document.frm["txt"+chknum]
document.frm["txtarea"+chk
document.frm["txt"+chknum]
document.frm["txtarea"+chk
}
}
in CF you will have to submit the form everytime to the same page when the checkbox is checked or unchecked and then put cfif conditions around the text box and textarea...
let me know if you want just the CF solution
Regards
Hart
ASKER
Thanks for your help Hart
The javascript function works well, I am still a little confused about how to add multiple check boxes "PS: in a loop form the check boxes with names as chk1, chk2, chk3 etc... and the corresponding text box and textarea names will also have 2,3,4 appended to it.. and in the function just send the number..."
(JavaScript isnt my greatest strength !) any code would be much appreciated to get my head around it !
Jonathan
The javascript function works well, I am still a little confused about how to add multiple check boxes "PS: in a loop form the check boxes with names as chk1, chk2, chk3 etc... and the corresponding text box and textarea names will also have 2,3,4 appended to it.. and in the function just send the number..."
(JavaScript isnt my greatest strength !) any code would be much appreciated to get my head around it !
Jonathan
ASKER
I also forgot to ask hart
how do you change the color of the form fields once they are disabled ?
Jonathan
how do you change the color of the form fields once they are disabled ?
Jonathan
ASKER
Got it working Hart, I apologise for my lack of common sense !! (LOL)
Just not sure how to color the form fields once they are disabled ?
Jonathan
Just not sure how to color the form fields once they are disabled ?
Jonathan
Its ok it happens with everyone in the start :-)
play with the following code and assign the colors u want :-)
<html>
<head>
<title>Enable / Disable</title>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function fncEnable(chknum)
{
if(document.frm["chk"+chkn um].checke d == true)
{
document.frm["txt"+chknum] .style.bac kground = 'FFFFFF';
document.frm["txt"+chknum] .disabled = false;
document.frm["txtarea"+chk num].style .backgroun d = 'FFFFFF';
document.frm["txtarea"+chk num].disab led = false;
}
else
{
document.frm["txt"+chknum] .style.bac kground = 'EEEEEE';
document.frm["txt"+chknum] .disabled = true;
document.frm["txtarea"+chk num].style .backgroun d = 'EEEEEE';
document.frm["txtarea"+chk num].disab led = true;
}
}
//-->
</SCRIPT>
</head>
<body>
<form name="frm" method="post" action="somepage name">
<input type="checkbox" name="chk1" value="1" OnClick="fncEnable(1)">
<input type="text" name="txt1" value="" DISABLED STYLE="background:EEEEEE">
<textarea rows="10" cols="10" name="txtarea1" disabled STYLE="background:EEEEEE"> </textarea >
</form>
</body>
</html>
Regards
Hart
play with the following code and assign the colors u want :-)
<html>
<head>
<title>Enable / Disable</title>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function fncEnable(chknum)
{
if(document.frm["chk"+chkn
{
document.frm["txt"+chknum]
document.frm["txt"+chknum]
document.frm["txtarea"+chk
document.frm["txtarea"+chk
}
else
{
document.frm["txt"+chknum]
document.frm["txt"+chknum]
document.frm["txtarea"+chk
document.frm["txtarea"+chk
}
}
//-->
</SCRIPT>
</head>
<body>
<form name="frm" method="post" action="somepage name">
<input type="checkbox" name="chk1" value="1" OnClick="fncEnable(1)">
<input type="text" name="txt1" value="" DISABLED STYLE="background:EEEEEE">
<textarea rows="10" cols="10" name="txtarea1" disabled STYLE="background:EEEEEE">
</form>
</body>
</html>
Regards
Hart
but do remember that the style attrib and the disabled options don't work in Netscape versions..
hope thats fine with u
Regards
Hart
hope thats fine with u
Regards
Hart
ASKER
Its okay hart its for a local intranet IE6 , im using CSS for styles
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hart the points are yours , thankyou for all your help
Best regards
Jonathan
Best regards
Jonathan
u r welcome :-)
regards
hart
regards
hart
Has anyone tried applying this function to a "image button"? I have been successful in using it for standard form elements but not image buttons.
Just make the image button invisible:
add these lines of code to your javascript
document.frm["imgbutton"+c hknum].sty le.dispaly = "none";
then on the else:
document.frm["imgbutton"+c hknum].sty le.dispaly = "block";
add these lines of code to your javascript
document.frm["imgbutton"+c
then on the else:
document.frm["imgbutton"+c
-------------------- javascript solution ----------------------
<html>
<head>
<title>Enable / Disable</title>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function fncEnable(chknum)
{
if(document.frm["chk"+chkn
{
document.frm["txt"+chknum]
document.frm["txtarea"+chk
}
else
{
document.frm["txt"+chknum]
document.frm["txtarea"+chk
}
}
//-->
</SCRIPT>
</head>
<body>
<form name="frm" method="post" action="somepage name">
<input type="checkbox" name="chk1" value="1" OnClick="fncEnable(1)">
<input type="text" name="txt1" value="" DISABLED>
<textarea rows="10" cols="10" name="txtarea1" disabled></textarea>
</form>
</body>
</html>
PS: in a loop form the check boxes with names as chk1, chk2, chk3 etc... and the corresponding text box and textarea names will also have 2,3,4 appended to it.. and in the function just send the number...
Regards
Hart