Show Field When Check Box Is Checked

S

Some Girl

I have a simple form that has one check box, an done text box. I'd
like the contents of the box to be hidden, but made visible if the
check box is checked.
Here's a snippet of code:

<form name="Form1" method="post">
<input name="View1" type="checkbox"><br>
<input name="View2" value="someanswer" type="hidden">
</form>
</script language="JavaScript">
if (document.Form1.View1.checked)
{
document.Form1.View2.style.visibility="visible";
}
</script>

Any suggestions?
 
E

Evertjan.

-Lost wrote on 04 jul 2007 in comp.lang.javascript:
Some said:
I have a simple form that has one check box, an done text box. I'd
like the contents of the box to be hidden, but made visible if the
check box is checked.
Here's a snippet of code:

<form name="Form1" method="post">
<input name="View1" type="checkbox"><br>
<input name="View2" value="someanswer" type="hidden">
</form>
</script language="JavaScript">
if (document.Form1.View1.checked)
{
document.Form1.View2.style.visibility="visible";
}
</script>

Use more accessible JavaScript and *understand* the markup you are
using. INPUT of type "hidden" does not hide an input. It creates a
hidden INPUT.

<input type="text" name="View2" style="visibility: hidden;" />
<!-- note the "style" attribute that specifies the visibility. -->

var v1 = document.forms['Form1'].elements['View1'];
var v2 = document.forms['Form1'].elements['View2'];
if (v1.checked) v2.style.visibility = 'visible';

v2.style.visibility = (v1.checked) ?'visible' :'hiddden';
 
Y

y2ee.asdf

-Lost wrote on 04 jul 2007 in comp.lang.javascript:


Use more accessible JavaScript and *understand* the markup you are
using. INPUT of type "hidden" does not hide an input. It creates a
hidden INPUT.
<input type="text" name="View2" style="visibility: hidden;" />
<!-- note the "style" attribute that specifies the visibility. -->
var v1 = document.forms['Form1'].elements['View1'];
var v2 = document.forms['Form1'].elements['View2'];
if (v1.checked) v2.style.visibility = 'visible';

v2.style.visibility = (v1.checked) ?'visible' :'hiddden';


Also, you realize, without encapsulating this in a function, the
script will always run inline, and never make "v2" visible. Put it in
a function, then fire it when "v1" is toggled, or by some other means
call it. (That may be what you meant your problem was.)

You would have to use either the "display:none" way or the
"visibility:hidden" way depending on how you want the page to look.
 
E

edwin.allan1

If I have multiple checkboxes and I want to be able to use the same
function for each one, how canit be done?
Do I replace (document.Form1.View1.checked) by
(document.Form1.this.name.checked) ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top