Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-11-2007, 12:22 AM   PM User | #1
ynotlim
Regular Coder

 
Join Date: Sep 2006
Posts: 131
Thanks: 5
Thanked 0 Times in 0 Posts
ynotlim is an unknown quantity at this point
Preselect in Javascript?

How can I preselect Jefferson and 44221 as the default select? See ancora's code below.

Thanks in advance,

Tony

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Any Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
	
	var zipCodes =[]; 
	zipCodes["counties"] = ["Summit","Jefferson"];
	zipCodes["Summit"] = ["44101","44107","44116","44135","44147"];
	zipCodes["Jefferson"] = ["44221","44266","44270"];
	
	function fillSelect(isValue,isNext){

		isNext.length = 1;
		var curr = zipCodes[isValue];
		for (each in curr)
			{
			 var nOption = document.createElement('option'); 
			 var isData = document.createTextNode(curr[each]); 
			 nOption.setAttribute('value',curr[each]); 
			 nOption.appendChild(isData); 
			 isNext.appendChild(nOption); 
			}
	}

	function init(){

		fillSelect('counties',document.forms[0]['county']);
	}
	
	onload=init;

</script>
<style type="text/css">

	 body {background-color:#eae3c6;margin-top:60px}
	 form {width:350px;margin:auto}
	 fieldset {background-color:#f0fff0;border:1px solid #87ceeb}
	 legend {font-family:times;font-size:14pt;color:#00008b;background-color:#87ceeb;padding-left:3px;padding-right:3px;margin-bottom:5px}
	 label {font-family:times;font-size:12pt;color:#00008B;padding:5px}
	 select {font-family:times;font-size:10pt;margin:10px}
	.submitBtn {display:block;margin-left:auto;margin-right:auto;margin-top:5px;margin-bottom:5px}

</style>
</head>
<body>
	<form method="post" action="">
	   <fieldset>
		<legend>Zip Code</legend>
			<select name='county' onchange="fillSelect(this.value,this.form['zipCode'])">
				<option selected>Select Your County</option>
			</select>
			<select name='zipCode'>
				<option selected >Select Your Zip Code</option>
			</select>
		<input type='submit' name='submit' value="Submit" class='submitBtn'>
	   </fieldset>
	</form>
</body>
</html>
__________________
---------
Client / Server Side Programmer II
ynotlim is offline   Reply With Quote
Old 01-11-2007, 01:29 AM   PM User | #2
6arredja
New Coder

 
Join Date: Nov 2004
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
6arredja is an unknown quantity at this point
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Any Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
	
	var zipCodes =[]; 
	zipCodes["counties"] = ["Summit","Jefferson"];
	zipCodes["Summit"] = ["44101","44107","44116","44135","44147"];
	zipCodes["Jefferson"] = ["44221","44266","44270"];
	
	function fillSelect(isValue,isNext){

		isNext.length = 1;
		var curr = zipCodes[isValue];
		for (each in curr)
			{
			 var nOption = document.createElement('option'); 
			 var isData = document.createTextNode(curr[each]);
			 if (each=="0"&&isValue=="Jefferson") {
			 nOption.selected="selected";
			 }
			 nOption.setAttribute('value',curr[each]); 
			 nOption.appendChild(isData); 
			 isNext.appendChild(nOption); 
			}
	}

	function init(){

		fillSelect('counties',document.forms[0]['county']);
	}
	
	onload=init;

</script>
<style type="text/css">

	 body {background-color:#eae3c6;margin-top:60px}
	 form {width:350px;margin:auto}
	 fieldset {background-color:#f0fff0;border:1px solid #87ceeb}
	 legend {font-family:times;font-size:14pt;color:#00008b;background-color:#87ceeb;padding-left:3px;padding-right:3px;margin-bottom:5px}
	 label {font-family:times;font-size:12pt;color:#00008B;padding:5px}
	 select {font-family:times;font-size:10pt;margin:10px}
	.submitBtn {display:block;margin-left:auto;margin-right:auto;margin-top:5px;margin-bottom:5px}

</style>
</head>
<body>
	<form method="post" action="">
	   <fieldset>
		<legend>Zip Code</legend>
			<select name='county' onchange="fillSelect(this.value,this.form['zipCode'])">
				<option selected>Select Your County</option>
			</select>
			<select name='zipCode'>
				<option selected>Select Your Zip Code</option>
			</select>
		<input type='submit' name='submit' value="Submit" class='submitBtn'>
	   </fieldset>
	</form>
</body>
</html>
did you want jefferson selected first too? or did you mean select jefferson and have a certain zipcode?

here is the specific code that i added

Code:
 if (each=="0"&&isValue=="Jefferson") {
			 nOption.selected="selected";
6arredja is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

Forum Jump


All times are GMT +1. The time now is 03:40 AM.


Advertisement
Log in to turn off these ads.