Avatar of EMB01
EMB01
Flag for United States of America asked on

Code to make fields invisible... (jQuery)

The following form should only be validated when fields are visible.  The other, "hidden" fields should not be validated.  My question is, how do I make fields "hidden?"  When a user selects "section one" from the dropdown menu, I want section one to be shown and all other sections to be hidden.  Likewise, if the user selects "option two," it will be shown and all other sections will be hidden.
<script type="text/javascript">

$().ready(function() {
	
	// validate signup form on keyup and submit
	$("#signupForm").validate({
                  ignore:":hidden",
		rules: {
			firstname: "required",
			lastname: "required",
			username: {
				required: true,
				minlength: 2
			}
		},
		messages: {
			firstname: "Please enter your firstname",
			lastname: "Please enter your lastname",
			username: {
				required: "Please enter a username",
				minlength: "Your username must consist of at least 2 characters"
			},
		}
	});

});

</script>

<form action="" method="post">

<select>
  <option value="volvo">Option one</option>
  <option value="saab">Option two</option>
</select>

<!-- BEGIN OPTION ONE -->

		<p>
			<label for="firstname">Firstname</label>
			<input id="firstname" name="firstname" />
		</p>
		<p>
			<label for="lastname">Lastname</label>
			<input id="lastname" name="lastname" />
		</p>

<!-- END OPTION ONE -->

<!-- BEGIN OPTION TWO -->

		<p>
			<label for="firstname">Firstname</label>
			<input id="firstname" name="firstname" />
		</p>
		<p>
			<label for="lastname">Lastname</label>
			<input id="lastname" name="lastname" />
		</p>

<!-- END OPTION TWO -->

</form>

Open in new window

JavaScript

Avatar of undefined
Last Comment
EMB01

8/22/2022 - Mon
leakim971

You may use :

		$("select").change(function() {
			$(".section").hide();
			var indexOfToShow = $("select>option:selected").index("select>option");
			$(".section:eq(" + indexOfToShow + ")").fadeIn("slow");
		});

Open in new window


Test page :


<!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 language="javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script type="text/javascript">

	$(document).ready(function() {
		
		// validate signup form on keyup and submit
/*		$("#signupForm").validate({
					  ignore:":hidden",
			rules: {
				firstname: "required",
				lastname: "required",
				username: {
					required: true,
					minlength: 2
				}
			},
			messages: {
				firstname: "Please enter your firstname",
				lastname: "Please enter your lastname",
				username: {
					required: "Please enter a username",
					minlength: "Your username must consist of at least 2 characters"
				},
			}
		});
*/		
		$("select").change(function() {
			$(".section").hide();
			var indexOfToShow = $("select>option:selected").index("select>option");
			$(".section:eq(" + indexOfToShow + ")").fadeIn("slow");
		});
		
	});

</script>
</head>
<body>
<form id="signupForm" action="" method="post">
<select>
  <option value="volvo">Option one</option>
  <option value="saab">Option two</option>
</select>
<!-- BEGIN OPTION ONE -->
<div class="section">
		<p>
			<label for="firstname">Firstname </label>
			<input id="firstname" name="firstname" />
		</p>
		<p>
			<label for="lastname">Lastname</label>
			<input id="lastname" name="lastname" />
		</p>
</div>
<!-- END OPTION ONE -->
<!-- BEGIN OPTION TWO -->
<div class="section">
		<p>
			<label for="firstname">Firstname</label>
			<input id="firstname" name="firstname" />
		</p>
		<p>
			<label for="lastname">Lastname</label>
			<input id="lastname" name="lastname" />
		</p>
</div>
<!-- END OPTION TWO -->
</form>
</body>
</html>

Open in new window

EMB01

ASKER
Thanks, that works well; however, when I uncomment the validation function it doesn't work anymore.  Can you help me make it work with the validator?
EMB01

ASKER
This is the validator I'm using, by the way:

http://bassistance.de/jquery-plugins/jquery-plugin-validation/
Your help has saved me hundreds of hours of internet surfing.
fblack61
leakim971

Check the depedencies section. Use jQuerie 1.3.2 or 1.4.2
EMB01

ASKER
I tried versions 1.4.2 and 1.4.4 but neither work.  Here's my code:
<!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 type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script src="jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">

	$(document).ready(function() {
		
		// validate signup form on keyup and submit
		$("#signupForm").validate({
					  ignore:":hidden",
			rules: {
				firstname: "required",
				lastname: "required",
				username: {
					required: true,
					minlength: 2
				}
			},
			messages: {
				firstname: "Please enter your firstname",
				lastname: "Please enter your lastname",
				username: {
					required: "Please enter a username",
					minlength: "Your username must consist of at least 2 characters"
				},
			}
		});
		
		$("select").change(function() {
			$(".section").hide();
			var indexOfToShow = $("select>option:selected").index("select>option");
			$(".section:eq(" + indexOfToShow + ")").fadeIn("slow");
		});
		
	});

</script>
</head>
<body>
<form id="signupForm" action="" method="post">
<select>
  <option value="volvo">Option one</option>
  <option value="saab">Option two</option>
</select>
<!-- BEGIN OPTION ONE -->
<div class="section">
		<p>
			<label for="firstname">Firstname </label>
			<input id="firstname" name="firstname" />
		</p>
		<p>
			<label for="lastname">Lastname</label>
			<input id="lastname" name="lastname" />
		</p>
</div>
<!-- END OPTION ONE -->
<!-- BEGIN OPTION TWO -->
<div class="section">
		<p>
			<label for="firstname">Firstname</label>
			<input id="firstname" name="firstname" />
		</p>
		<p>
			<label for="lastname">Lastname</label>
			<input id="lastname" name="lastname" />
		</p>
</div>
<!-- END OPTION TWO -->
</form>
</body>
</html>

Open in new window

leakim971

Work fine for me :
<!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 language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script src="jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">

	$(document).ready(function() {
		
		// validate signup form on keyup and submit
		$("#signupForm").validate({
					  ignore:":hidden",
			rules: {
				firstname: "required",
				lastname: "required",
				username: {
					required: true,
					minlength: 2
				}
			},
			messages: {
				firstname: "Please enter your firstname",
				lastname: "Please enter your lastname",
				username: {
					required: "Please enter a username",
					minlength: "Your username must consist of at least 2 characters"
				},
			}
		});

		$("select").change(function() {
			$(".section").hide();
			var indexOfToShow = $("select>option:selected").index("select>option");
			$(".section:eq(" + indexOfToShow + ")").fadeIn("slow");
		}).change();
		
	});

</script>
</head>
<body>
<form id="signupForm" action="http://www.experts-exchange.com" method="post">
<select>
  <option value="volvo">Option one</option>
  <option value="saab">Option two</option>
</select><input type="submit" value="Submit" />
<!-- BEGIN OPTION ONE -->
<div class="section">
		<p>
			<label for="firstname">Firstname </label>
			<input id="firstname" name="firstname" />
		</p>
		<p>
			<label for="lastname">Lastname</label>
			<input id="lastname" name="lastname" />
		</p>
</div>
<!-- END OPTION ONE -->
<!-- BEGIN OPTION TWO -->
<div class="section">
		<p>
			<label for="firstname">Firstname</label>
			<input id="firstname" name="firstname" />
		</p>
		<p>
			<label for="lastname">Lastname</label>
			<input id="lastname" name="lastname" />
		</p>
</div>
<!-- END OPTION TWO -->
</form>
</body>
</html>

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
EMB01

ASKER
Really?  I copied and pasted your code and it doesn't work at all for me...  I attached a screenshot to show you what it does.
ss030811.jpg
leakim971

Try to clear your cache. Tested on IE8 :
leakim971-417779.flv
EMB01

ASKER
You're right, it does work in IE but not the version of the browser I'm using (IE Version: 8.0.6001.18702).  This is the first time I've ever had something not work in this browser; it's the only browser I ever use.  Do you know of anyway we could fix this?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
leakim971

Did you cleared your cache? I've the same version.
Clipboard02.gif
EMB01

ASKER
I tried clearing my cache but it still won't work.  The browser that I use is that version of IE, but it's called slimbrowser:

http://www.slimbrowser.net/en/

It's a really great browser, and I've never had something fail in it before.  Does this mean the script won't work in any previous versions of IE?  Is there anyway to make this cross-browser?
leakim971

Could you confirm the jquery.validate plugin is in the same folder of the page ?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
EMB01

ASKER
Yes, it's in the same folder.  Maybe, can we make the script simpler so that it works on older browsers?  I'm just guessing...
leakim971

We're using jQuery... Especially for browser compatibilty
I run a test on http://browsershots.org
Currently it look fine on most of browsers.
In slimbrowser it look like JS is not enabled...
EMB01

ASKER
Hmm...  I was working until I uncommented the validation part.  There must be some sort of incompatibility between the two.  Perhaps, there's another way of writing the same code?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
leakim971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
EMB01

ASKER
Thanks so much!  That worked very well.  I can't spot what's different, though.  I'll have to give it a closer inspection.