Forum Moderators: not2easy

Message Too Old, No Replies

How Toggle Hide/Show with a check box in a form

         

JoeVodkaSauce

5:41 pm on Oct 26, 2007 (gmt 0)

10+ Year Member



Sorry for the silly question but I'm stumped. I've setup (with a lot of help from this forum) a little form which has 2 radio buttons - and depending upon which is selected a div element is shown or hidden. I am trying to change this into a single form element check box - not multiple radio buttons so if it is checked something shows - if it is "un-checked" then nothing shows. For the Radio buttons I was using onClick and onBlur to achieve this. I can't for the life of me get this working. Here's the code I've setup for the radio buttons - working fine. Any suggestions on how to get this same thing implemented for a single check box (toggle on - show content/toggle off - hide content). Thanks in advance!


<HTML>
<head>
<style type="text/css">
#addSubscriptionHidden {
display:none;
}
#removeSubscriptionHidden {
display:none;
}
</style>
</head>
<body>
Add Subscription(s)
<input type="radio" name="subscription" id="addSubscription" onClick="document.getElementById('addSubscriptionHidden').style.display='block'" onBlur="document.getElementById('addSubscriptionHidden').style.display='none'">
Remove Subscription(s)<input type="radio" name="subscription" id="removeSubscription" onClick="document.getElementById('removeSubscriptionHidden').style.display='block'" onBlur="document.getElementById('removeSubscriptionHidden').style.display='none'">
<br>
<br>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td><div id=addSubscriptionHidden>
Add Subscription Form
</div>
<div id=removeSubscriptionHidden>
Remove Subscription Form
</div></td>
</tr>
</table>
</body>
</html>

JoeVodkaSauce

6:28 pm on Oct 26, 2007 (gmt 0)

10+ Year Member



Even cleaner version of what I'm trying to do - this one properly shows the content when the checkbox is clicked but when I un-check it the content stays. I know there si somethign obvisou I'm missing here.


<html>
<head>
<style type="text/css">
#addSubscriptionHidden {
display:none;
}
</style>
</head>

<body>
Add Subscription(s)
<input type="checkbox" name="subscription" id="addSubscription" onClick="document.getElementById('addSubscriptionHidden').style.display='block'">
<br>
<br>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td><div id=addSubscriptionHidden>
Add Subscription Form
</div>
</td>
</tr>
</table>
</body>
</html>

medic325

9:18 pm on Oct 26, 2007 (gmt 0)

10+ Year Member



This post should probably be in the JavaScript forum, but!

In your second example, you're on the right track, but you need some code to check to see whether or not the checkbox is checked after the user clicks.

For example:

<input type="checkbox" name="subscription" id="addSubscription" onclick="toggle();">

Add this to your <head> section (sorry for the spacing issues, the forum eats the formatting even when surrounded by code tags):

<script type="text/javascript">
function toggle() {
if (document.getElementById) {
if (document.getElementById("addSubscription").checked == true) {
document.getElementById("addSubscriptionHidden").style.display = "block";
} else {
document.getElementById("addSubscriptionHidden").style.display = "none";
}
}
}
</script>

This is really rough and should be cleaned up (get rid of inline event handlers, etc.) before you put it in production, but I think it does what you want?

JoeVodkaSauce

12:12 am on Oct 27, 2007 (gmt 0)

10+ Year Member



Thanks for the info! I did eventually find a javascript way of doing it - I was just hoping to do all CSS - but again - I'm not what you'd call a developer so what do I know. I like the sample you provided better then what I'm using.

I presume this type of function can't really be accomplished with pure CSS then? I was afraid of customers who may have Javascript disabled not being able to utilize this function.

Thanks again!