JavaScript

Moderators: None (Apply to moderate this forum)
Number of threads: 2061
Number of posts: 5164

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Get values from twodimensional array Posted by treshr on 28 Nov 2005 at 3:40 AM
This message was edited by treshr at 2005-11-28 3:48:0

I made this script:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="JavaScript">

var f = document.forms['test'];

var l = f.land.options.length;
var provincie = new Array(l);

for(i = 0; i < l.length; i++){
	provincie[i] = new Array();
}

provincie[0][0] = "Kies eerst een land";

provincie[1][0] = "Kies een provincie";
provincie[1][1] = "Groningen";
provincie[1][2] = "Friesland";
provincie[1][3] = "Drenthe";
provincie[1][4] = "Gelderland";

var t = f.provincie;

function zetProvincies(x){
	for(i = 0; i < provincie[x].length; i++){
		t.options[i] = new Option(provincie[x][i], provincie[x][i]);
	}
}

</script> 
</head>
<body>
<form id="test" name="test" method="post" action="" >
<table>
<tr>
	<td ><font class="content">Land:</font></td>
	<td>
		<select name="land" size="1" onChange="zetProvincies(this.options.selectedIndex)">
			<option value="0" selected>Geen Selectie</option>
			<option value="1">Nederland</option>
		</select>
	</td>
</tr>
<tr>
	<td><font class="content">Provincie:</font></td>
	<td>
		<select name="provincie">

		</select>
	</td>
</tr>
</table>
</form>
</body>
</html>


And i get the error that the provincie array is empty or is no object. Does anybody what I did wrong. I can do some things with Javascript but this I can't solve on my own.

Thanx in advance.

--=][tReShR][=--








Report
Re: Get values from twodimensional array Posted by NCS_One on 28 Nov 2005 at 5:08 AM
: This message was edited by treshr at 2005-11-28 3:48:0

: I made this script:
:
: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
: <html>
: <head>
: <script language="JavaScript">
: 
: var f = document.forms['test'];
: 
: var l = f.land.options.length;
: var provincie = new Array(l);
: 
: for(i = 0; i < l.length; i++){
: 	provincie[i] = new Array();
: }
: 
: provincie[0][0] = "Kies eerst een land";
: 
: provincie[1][0] = "Kies een provincie";
: provincie[1][1] = "Groningen";
: provincie[1][2] = "Friesland";
: provincie[1][3] = "Drenthe";
: provincie[1][4] = "Gelderland";
: 
: var t = f.provincie;
: 
: function zetProvincies(x){
: 	for(i = 0; i < provincie[x].length; i++){
: 		t.options[i] = new Option(provincie[x][i], provincie[x][i]);
: 	}
: }
: 
: </script> 
: </head>
: <body>
: <form id="test" name="test" method="post" action="" >
: <table>
: <tr>
: 	<td ><font class="content">Land:</font></td>
: 	<td>
: 		<select name="land" size="1" onChange="zetProvincies(this.options.selectedIndex)">
: 			<option value="0" selected>Geen Selectie</option>
: 			<option value="1">Nederland</option>
: 		</select>
: 	</td>
: </tr>
: <tr>
: 	<td><font class="content">Provincie:</font></td>
: 	<td>
: 		<select name="provincie">
: 
: 		</select>
: 	</td>
: </tr>
: </table>
: </form>
: </body>
: </html>
: 

:
: And i get the error that the provincie array is empty or is no object. Does anybody what I did wrong. I can do some things with Javascript but this I can't solve on my own.
:
: Thanx in advance.
:
: --=][tReShR][=--
:
:
:
:
:
:
:
:
:

Hi

Try this post :
http://programmersheaven.com/c/MsgBoard/read.asp?Board=35&MsgID=320094&Setting=A9999F0002
Report
Re: Get values from twodimensional array Posted by treshr on 28 Nov 2005 at 5:48 AM
That doesn't help me. I don't have the question how to create a twodimensional array but why my script keeps saying that there is no object or that it's empty.


--=][tReShR][=--

Report
Re: Get values from twodimensional array Posted by NCS_One on 28 Nov 2005 at 8:31 AM
This message was edited by NCS_One at 2005-11-28 8:32:5

: That doesn't help me. I don't have the question how to create a twodimensional array but why my script keeps saying that there is no object or that it's empty.
:
:
: --=][tReShR][=--
:
:

Hi

You are creating the array in the wrong way, you have to do it like this :
var provincie = [[1],[4]];

Hope it helps.


Report
Re: Get values from twodimensional array Posted by Bodkin on 18 Dec 2005 at 11:16 AM
This rather large code works like this:
In stead of filling a new select list every time you make a choice of country, fill a lot of select lists and display only the one chosen.

I guess it's a bit more complicated than your code needs to be if you are only choosing country once for each session.

PS. Please notice the difference between COUNTY and COUNTRY


<html><head>
<script>
p = new Array();
p[0] = [
	"Kies een provincie",
	"Groningen",
	"Friesland",
	"Drenthe",
	"Gelderland"
];
p[0].country = "Nederland";

p[1] = [
	"Choose a county",
	"Bedfordshire",
	"Berkshire",
	"Buckinghamshire",
	"Cambridgeshire",
	"Cheshire",
	"Cornwall",
	"Cumberland",
	"Derbyshire",
	"Devon",
	"Dorset",
	"Durham",
	"Essex",
	"Gloucestershire",
	"Hampshire",
	"Herefordshire",
	"Hertfordshire",
	"Huntingdonshire",
	"Kent",
	"Lancashire",
	"Leicestershire",
	"Lincolnshire",
	"Middlesex",
	"Norfolk",
	"Northamptonshire",
	"Northumberland",
	"Nottinghamshire",
	"Oxfordshire",
	"Rutland",
	"Shropshire",
	"Somerset",
	"Staffordshire",
	"Suffolk",
	"Surrey",
	"Sussex",
	"Warwickshire",
	"Westmorland",
	"Wiltshire",
	"Worcestershire",
	"Yorkshire",
	"Anglesey/Sir Fon",
	"Brecknockshire/Sir Frycheiniog",
	"Caernarfonshire/Sir Gaernarfon",
	"Carmarthenshire/Sir Gaerfyrddin",
	"Cardiganshire/Ceredigion",
	"Denbighshire/Sir Ddinbych",
	"Flintshire/Sir Fflint",
	"Glamorgan/Morgannwg",
	"Merioneth/Meirionnydd",
	"Monmouthshire/Sir Fynwy",
	"Montgomeryshire/Sir Drefaldwyn",
	"Pembrokeshire/Sir Benfro",
	"Radnorshire/Sir Faesyfed",
	"Aberdeenshire",
	"Angus/Forfarshire",
	"Argyllshire",
	"Ayrshire",
	"Banffshire",
	"Berwickshire",
	"Buteshire",
	"Cromartyshire",
	"Caithness",
	"Clackmannanshire",
	"Dumfriesshire",
	"Dunbartonshire/Dumbartonshire",
	"East Lothian/Haddingtonshire",
	"Fife",
	"Inverness-shire",
	"Kincardineshire",
	"Kinross-shire",
	"Kirkcudbrightshire",
	"Lanarkshire",
	"Midlothian/Edinburghshire",
	"Morayshire",
	"Nairnshire",
	"Orkney",
	"Peeblesshire",
	"Perthshire",
	"Renfrewshire",
	"Ross-shire",
	"Roxburghshire",
	"Selkirkshire",
	"Shetland",
	"Stirlingshire",
	"Sutherland",
	"West Lothian/Linlithgowshire",
	"Wigtownshire"];
p[1].country = "Great Britain";

var displayedSelect = null;
function fillSelect() {
	sel = document.getElementById("SELECT");
	// Fill country SELECT
	country = document.getElementById("COUNTRY");
	opt = document.createElement("OPTION");
	opt.innerHTML = "Choose a country";
	country.appendChild(opt);

	for(s=0;s<p.length;s++) {
		opt = document.createElement("OPTION");
		opt.innerHTML = p[s].country;
		country.appendChild(opt);

		// Fill county SELECTs
		county = document.createElement("SELECT");
		county.onchange = selectCounty;
		county.id = p[s].country;
		county.style.display = "none";
		for(t=0;t<p[s].length;t++) {
			opt = document.createElement("OPTION");
			opt.innerHTML = p[s][t];
			county.appendChild(opt);
		}
		sel.appendChild(county);
	}
	displayedSelect = document.getElementById("COUNTY");
}
function selectCounty() {
	alert("selected county:
	"+displayedSelect.options[displayedSelect.options.selectedIndex].text);
}
function selectCountry(e) {
	displayedSelect.style.display = "none";
	if(e==0)
		displayedSelect = document.getElementById("COUNTY");
	else
		displayedSelect = document.getElementById(p[parseInt(e)-1].country);
	displayedSelect.style.display = "block";
}
</script>
</head><body onload="fillSelect()">

<form id="test" name="test" method="post" action="" >
<table>

<tr>
<td>Country:
<td><select id="COUNTRY" onChange = "selectCountry(this.options.selectedIndex)"></select>

<tr><td>County:
<td>
<div id="SELECT"></div>
<div id="COUNTY" style="display:block">No country selected</div>

</table></form>

</body>
</html>
[SIZE]




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.