#1 (permalink)  
Old 08-01-2008, 11:59 AM
Registered User
 
Join Date: Aug 2008
Posts: 2
show/hide form fields

Hello,

I've created a form that has several checkboxes and then some questions related to the checkboxes. What I would like is for the related questions to be hidden until the checkbox is selected, then the addition questions for - that check box only - would appear. Is this something that can be done in javascript and can someone help me figure out how to build it?

Here's what I have so far:
<code>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Badgering</title>
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){

//Hide div w/id extra
$("#extra").css("display","none");

// Add onclick handler to checkbox w/id checkme
$("#checkme").click(function(){

// If checked
if ($("#checkme").is(":checked"))
{
//show the hidden div
$("#extra").show("fast");
}
else
{
//otherwise, hide it
$("#extra").hide("fast");
}
});

});
</script>
<style type="text/css">
<!--
label {
display: block;
}
-->
</style>

</head>

<body>
<form>
<table>
<tr><td class=m><label for "Badgering">Badgering</label></td>
<td class=m><input type=checkbox size=10 name=_fid_12 />
<div id="extra">
<table>
<tr>
<td width="15"></td>
<td width="85">Timestamp(s)</td>
<td class="m" width="288"><input type="text" size="40" name="_fid_28"></td>
</tr>
</table>
</td>
</tr>
</div>
</table>
</form>
</body>


</html>
</code>

Thanks,
Lori

Last edited by Lori; 08-01-2008 at 01:47 PM. Reason: Adding code
Reply With Quote

  #2 (permalink)  
Old 08-01-2008, 08:12 PM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,437
It sure can. Here's basically what you would do:

Code:
<input type="checkbox" name="cb" id="cb1" value="1" onchange="showfield(this)" />
<input type="checkbox" name="cb" id="cb2" value="2" onchange="showfield(this)" />
<input type="text" name="text1" id="text1" />
<input type="text" name="text2" id="text2" />
<script type="text/javascript" language="javascript">
function showfield(what) {
if(what.id = "cb1" && what.checked) {
document.getElementById('text1').style.display = "inline";
}
else if(what.id = "cb1" && !what.checked) {
document.getElementById('text1').style.display = "none";
}
else if(what.id = "cb2" && what.checked) {
document.getElementById('text2').style.display = "inline";
}
else if(what.id = "cb2" && !what.checked) {
document.getElementById('text2').style.display = "none";
}
else {
document.getElementById('text1').style.display = "none";
document.getElementById('text2').style.display = "none";
}
}
</script>
I haven't tested that code, but it should work. It's not the most efficient or effective function I could've written, but it's decent enough to get you started.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
  #3 (permalink)  
Old 08-04-2008, 08:18 AM
Registered User
 
Join Date: Aug 2008
Posts: 2
Thanks but...

I'm getting syntax errors returned and I'm not able to make it work. I imagine it's something to do with how I've incorporated the code into my form as your code looks fine to me. Here's what I'm doing minus much of the normal HTML:

Code:
<script type="text/javascript">
<input type="checkbox" name="cb" id="cb1" value="1" onchange="showfield(this)" />
<input type="checkbox" name="cb" id="cb2" value="2" onchange="showfield(this)" />
<input type="text" name="text1" id="text1" />
<input type="text" name="text2" id="text2" />
<script type="text/javascript" language="javascript">
function showfield(what) {
if(what.id = "cb1" && what.checked) {
document.getElementById('text1').style.display = "inline";
}
else if(what.id = "cb1" && !what.checked) {
document.getElementById('text1').style.display = "none";
}
else if(what.id = "cb2" && what.checked) {
document.getElementById('text2').style.display = "inline";
}
else if(what.id = "cb2" && !what.checked) {
document.getElementById('text2').style.display = "none";
}
else {
document.getElementById('text1').style.display = "none";
document.getElementById('text2').style.display = "none";
}
}
</script>

<table>
	<tr>
		<td class=m>Badgering</td>
		<td class=m><input type=checkbox size=10 name=cb1>	
		<td>Timestamp(s)</td>
		<td class="m"><input type="text" size="40" name="text1"></td>	
		
	</tr>
</div>
<tr>
	<td class=m>Credit(s) Promised</td>
	<td class=m><input type=checkbox size=10 name=cb2>
			<td width="85">Timestamp(s)</td>
			<td class=m><input type="text" size="40" name="text2"></td>
			<td class=m>Credit Amount</td>
			<td class=m><input type=text size=10 name=_fid_14 ></td>
			<td class=m>Credit Applied</td>

			<td class=m><select  name=_fid_15 >
				<option></option>
				<option>Yes</option>
				<option>No</option>
			</select>
		</td>
		
</tr>

</table>
Thanks again
Reply With Quote
  #4 (permalink)  
Old 08-04-2008, 05:04 PM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,437
The problem is within the code I provided, which carried over to the code you are using.

Sorry, I sometimes forget which language I'm coding in, and use the wrong constructs.

Try it this way:
Code:
<script type="text/javascript" language="javascript">
function showfield(what) {
if(what.id == "cb1" && what.checked) {
document.getElementById('text1').style.display = "inline";
}
else if(what.id == "cb1" && !what.checked) {
document.getElementById('text1').style.display = "none";
}
else if(what.id == "cb2" && what.checked) {
document.getElementById('text2').style.display = "inline";
}
else if(what.id == "cb2" && !what.checked) {
document.getElementById('text2').style.display = "none";
}
else {
document.getElementById('text1').style.display = "none";
document.getElementById('text2').style.display = "none";
}
}
</script>
In javascript, a single equal sign will assign the value on the right to the variable on the left. In order to test equivalency, you need two equal signs, together.

Doing a lot of work in VBScript, recently, which uses a single equal sign for everything.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
auto submit a form? possible? Allen Programming and Scripting 1 08-04-2006 03:14 AM
Form fields cswallow Programming and Scripting 1 08-03-2006 12:41 PM
Setting Values For Form Elements With Unconventional Names curtiss Programming and Scripting 2 06-04-2006 09:28 PM
Obtaining a Form Elements Index by Name curtiss Programming and Scripting 3 05-02-2006 11:25 AM
passing information from 1 form to another amanxman Programming and Scripting 3 04-12-2006 04:59 PM


All times are GMT -5. The time now is 07:40 PM.

 
Bitrix
Clicky Web Analytics
CloudContacts
Maxtango


Subscribe to our feed | add to myYahoo!

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
© 1997-2007 HTMLCenter