close ad
Install the LAtest Updates to Work with CC 2017 and CC 2018
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Show textarea if checkbox is checked?

Thread began 10/14/2013 4:41 am by Nathon Jones Web Design | Last modified 10/16/2013 12:58 pm by Nathon Jones Web Design | 2965 views | 8 replies |

Nathon Jones Web Design

Show textarea if checkbox is checked?

Is it possible to hide a textarea until a user puts a check in a checkbox using Validation Toolkit or does a Submit button need to be pressed?
Here's an example on a site we're developing:
http://www.nathonjones.com/babyfish2013/personalised-baby-toddler-gifts.asp?BFid=BF570

Thanks for any advice offered:
NJ

Sign in to reply to this post

Jason ByrnesWebAssist

here is a very basic example to demoinstrate how this can be done:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function toggleTextArea() {
var theForm = document.form1;
var theCheck = theForm.checkbox;
var theTextArea = theForm.textfield2;
if(theCheck.checked == true) {
theTextArea.style.display = "block";
} else {
theTextArea.style.display = "none";
}
}
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<label for="textfield"></label>
<input type="text" name="textfield" id="textfield" />
<input name="checkbox" type="checkbox" id="checkbox" onchange="toggleTextArea();" value="1"/>
<label for="checkbox"></label>
<label for="textfield2"></label>
<textarea style="display: none;" name="textfield2" rows="8" id="textfield2"></textarea>
</form>
</body>
</html>
Sign in to reply to this post

Nathon Jones Web Design

IE8

Thank you Jason. That doesn't work in IE8, but I assume it works in all modern browsers.
Is there a way to hide the label text until the checkbox has been checked or can you only hide/reveal the textarea itself?

Really appreciate it. That's the only real hold up with this project.
Thank you, again.
NJ

Sign in to reply to this post

Jason ByrnesWebAssist

you could put the text area into a span, and have the JS function change the style of the span by ID.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function toggleTextArea() {
var theForm = document.form1;
var theCheck = theForm.checkbox;
var theTextArea = document.getElementById('textAreaSpan');
if(theCheck.checked == true) {
theTextArea.style.display = "block";
} else {
theTextArea.style.display = "none";
}
}
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<label for="textfield"></label>
<input type="text" name="textfield" id="textfield" />
<input name="checkbox" type="checkbox" id="checkbox" onchange="toggleTextArea();" value="1"/>
<label for="checkbox"></label>
<span style="display: none;" id="textAreaSpan"><label for="textfield2">label for text area</label>
<textarea name="textfield2" rows="8" id="textfield2"></textarea></span>
</form>
</body>
</html>
Sign in to reply to this post

Nathon Jones Web Design

Great stuff!

Really great help, thank you Jason.
NJ

Sign in to reply to this post

Jason ByrnesWebAssist

you're welcome.

Sign in to reply to this post

Nathon Jones Web Design

IE

Sorry, that's not working in Internet Explorer I just realised! Have tried it in IE8 and IE10.
What happens is that the textarea doesn't display until you click somewhere else on the page.
Have I done something wrong or were you aware of that incompatibility?

Thank you.
NJ

Sign in to reply to this post

Jason ByrnesWebAssist

I think early IE versions have an issue with the onchange event when used with checkboxes and radio buttons, try using onclick instead.

change:
onchange="toggleTextArea();"

to:
onclick="toggleTextArea();"

Sign in to reply to this post

Nathon Jones Web Design

IE

It wasn't working in IE10 either. Onclick has resolved it though. Much appreciated.
NJ

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...