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 08-21-2011, 09:54 AM   PM User | #1
linguist2000
New to the CF scene

 
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
linguist2000 is an unknown quantity at this point
Can anyone help please

html code is like this
Code:
<html>
<head>
<script language="jscript" src="cities.js" type="text/jscript"></script>
<script language="javascript" type="text/javascript">
    function onLoad() {
    try {FillCitiesAndCounties("_cityID", "_countyID", 0, 0);} catch(ex) {}
    }
    window.attachEvent("onload", onLoad);
</script>
</head>

<body>
<form>
<tr>
<td><label>City</label></td>
<td><select id="_cityID" name="cityid" style="width:100%;"><option value="0">(All)</option></select></td>
<td><label>County</label></td>
<td><select id="_countyID" name="countyid" style="width:100%;"></select></td>
</tr>
</form>
</body>

And the cities.js is like this:
Code:
var cities = new Array("", "America", "England", "Germany");
var counties = new Array();
counties[0] = new Array("");
counties[1] = new Array("", "New York", "Washington");
counties[2] = new Array("London", "WEstminister");
counties[3] = new Array("Berlin", "Kohln");


function SortByName(arr, start) {
	var result = new Array();
	for(var i = start; i < arr.length; i++)
		result.push({Id: i, Name: arr[i]});
	result = result.sort(function() {
		return arguments[0].Name.localeCompare(arguments[1].Name);
	});
	return result;
}


function FillCitiesAndCounties() {
	var sel1 = document.getElementById(arguments[0]), sel2 = document.getElementById(arguments[1]);
	var cityId = (arguments[2] != null) ? arguments[2] : 1, countyId = arguments[3] ? arguments[3] : 0;
	var arr = SortByName(cities, 1);
	for(var i = 0; i < arr.length; i++)
		sel1.addOption(arr[i].Id, arr[i].Name, arr[i].Id == cityId);
	sel2.addOption(0, "", countyId == 0);
	arr = SortByName(counties[cityId], counties[cityId][0] == "" ? 1 : 0);
	for(var i = 0; i < arr.length; i++)
		sel2.addOption((cityId * 100) + arr[i].Id, arr[i].Name, countyId == (cityId * 100) + arr[i].Id);
	sel1.attachEvent("onchange", function() {
		while(sel2.options.length > 0)
			sel2.options.remove(0);
		sel2.addOption(0, "", true);
		var cityId = parseInt(event.srcElement.options[event.srcElement.selectedIndex].value);
		var arr = SortByName(counties[cityId], counties[cityId][0] == "" ? 1 : 0);
		for(var i = 0; i < arr.length; i++)
			sel2.addOption((cityId * 100) + arr[i].Id, arr[i].Name, false);

	});
}
Nothing seems wrong but it doesnt work
Attached Files
File Type: zip example.zip (1.1 KB, 22 views)

Last edited by linguist2000; 08-21-2011 at 09:58 AM.. Reason: add files
linguist2000 is offline   Reply With Quote
Old 08-21-2011, 02:19 PM   PM User | #2
Arnaud
Regular Coder

 
Join Date: Jan 2008
Location: Geneva, Switzerland
Posts: 413
Thanks: 12
Thanked 29 Times in 29 Posts
Arnaud is on a distinguished road
Don't expect people to help with such poor information. If at least your code would be commented... Nothing seems wrong? Are you sure?
__________________
Chuck Norris counted to infinity.
Twice.
Arnaud is offline   Reply With Quote
Old 08-21-2011, 07:02 PM   PM User | #3
dgonz
New to the CF scene

 
Join Date: Aug 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
dgonz is an unknown quantity at this point
What it is you're trying to do, and what is this not doing for you - exactly?
dgonz is offline   Reply With Quote
Old 08-21-2011, 09:29 PM   PM User | #4
linguist2000
New to the CF scene

 
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
linguist2000 is an unknown quantity at this point
it doesnt fill cities when the page starts
linguist2000 is offline   Reply With Quote
Old 08-22-2011, 07:50 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,035
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Have you tried using Firebug?

Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. You can (and should) edit it to make it more meaningful.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 08-22-2011, 07:53 AM   PM User | #6
whitecouncil
New to the CF scene

 
Join Date: Aug 2011
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
whitecouncil is an unknown quantity at this point
please exactly state what is your problem so that we may help you better, on first look the coding seems right to me
whitecouncil is offline   Reply With Quote
Old 08-22-2011, 10:40 AM   PM User | #7
linguist2000
New to the CF scene

 
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
linguist2000 is an unknown quantity at this point
Quote:
Originally Posted by whitecouncil View Post
please exactly state what is your problem so that we may help you better, on first look the coding seems right to me
the problem is: it doesnt fill cities from array when the page starts
linguist2000 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 09:10 AM.


Advertisement
Log in to turn off these ads.