473,763 Members | 5,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dependent coutry city drop down box

gk
i want to have 2 dpendent drown box. one for country , and the other
for city.

if you change country in box1 , the city list will also change in
box2..
How to do it in javascript ?

can you provide an example with few dumy country name and their cities.

Thank you

Mar 1 '06 #1
2 2608
Here is some code that may help you:

<HEAD>

<script type="text/javascript">
<!--
var arrItems1 = new Array();
var arrItemsGrp1 = new Array();

arrItems1[3] = "Truck";
arrItemsGrp1[3] = 1;
arrItems1[4] = "Train";
arrItemsGrp1[4] = 1;
arrItems1[5] = "Car";
arrItemsGrp1[5] = 1;

arrItems1[6] = "Boat";
arrItemsGrp1[6] = 2;
arrItems1[7] = "Submarine" ;
arrItemsGrp1[7] = 2;

arrItems1[0] = "Planes";
arrItemsGrp1[0] = 3;
arrItems1[1] = "Ultralight ";
arrItemsGrp1[1] = 3;
arrItems1[2] = "Glider";
arrItemsGrp1[2] = 3;

var arrItems2 = new Array();
var arrItemsGrp2 = new Array();

arrItems2[21] = "747";
arrItemsGrp2[21] = 0
arrItems2[22] = "Cessna";
arrItemsGrp2[22] = 0

arrItems2[31] = "Kolb Flyer";
arrItemsGrp2[31] = 1
arrItems2[34] = "Kitfox";
arrItemsGrp2[34] = 1

arrItems2[35] = "Schwietzer Glider";
arrItemsGrp2[35] = 2

arrItems2[99] = "Chevy Malibu";
arrItemsGrp2[99] = 5
arrItems2[100] = "Lincoln LS";
arrItemsGrp2[100] = 5
arrItems2[57] = "BMW Z3";
arrItemsGrp2[57] = 5

arrItems2[101] = "F-150";
arrItemsGrp2[101] = 3
arrItems2[102] = "Tahoe";
arrItemsGrp2[102] = 3

arrItems2[103] = "Freight Train";
arrItemsGrp2[103] = 4
arrItems2[104] = "Passenger Train";
arrItemsGrp2[104] = 4

arrItems2[105] = "Oil Tanker";
arrItemsGrp2[105] = 6
arrItems2[106] = "Fishing Boat";
arrItemsGrp2[106] = 6

arrItems2[200] = "Los Angelas Class";
arrItemsGrp2[200] = 7
arrItems2[201] = "Kilo Class";
arrItemsGrp2[201] = 7
arrItems2[203] = "Seawolf Class";
arrItemsGrp2[203] = 7

function selectChange(co ntrol, controlToPopula te, ItemArray,
GroupArray) {
var myEle ;
var x ;
// Empty the second drop down box of any choices
for (var q=controlToPopu late.options.le ngth;q>=0;q--)
controlToPopula te.options[q]=null;
if (control.name == "firstChoic e") {
// Empty the third drop down box of any choices
for (var q=form.thirdCho ice.options.len gth;q>=0;q--)
form.thirdChoic e.options[q] = null;
}
// ADD Default Choice - in case there are no values
myEle = document.create Element("option ") ;
myEle.value = 0 ;
myEle.text = "[SELECT]" ;
// controlToPopula te.add(myEle) ;
controlToPopula te.appendChild( myEle)
// Now loop through the array of individual items
// Any containing the same child id are added to
// the second dropdown box
for ( x = 0 ; x < ItemArray.lengt h ; x++ ) {
if ( GroupArray[x] == control.value ) {
myEle = document.create Element("option ") ;
//myEle.value = x ;
myEle.setAttrib ute('value',x);
// myEle.text = ItemArray[x] ;
var txt = document.create TextNode(ItemAr ray[x]);
myEle.appendChi ld(txt)
// controlToPopula te.add(myEle) ;
controlToPopula te.appendChild( myEle)
}
}
}

function selectChange(co ntrol, controlToPopula te, ItemArray,
GroupArray) {
var myEle ;
var x ;
// Empty the second drop down box of any choices
for (var q=controlToPopu late.options.le ngth;q>=0;q--)
controlToPopula te.options[q]=null;
if (control.name == "firstChoic e") {
// Empty the third drop down box of any choices
for (var q=form.thirdCho ice.options.len gth;q>=0;q--)
form.thirdChoic e.options[q] = null;
}
// ADD Default Choice - in case there are no values
myEle=document. createElement(" option");
theText=documen t.createTextNod e("[SELECT]");
myEle.appendChi ld(theText);
myEle.setAttrib ute("value","0" );
controlToPopula te.appendChild( myEle);
// Now loop through the array of individual items
// Any containing the same child id are added to
// the second dropdown box
for ( x = 0 ; x < ItemArray.lengt h ; x++ ) {
if ( GroupArray[x] == control.value ) {
myEle = document.create Element("option ") ;
//myEle.value = x ;
myEle.setAttrib ute("value",x);
// myEle.text = ItemArray[x] ;
var txt = document.create TextNode(ItemAr ray[x]);
myEle.appendChi ld(txt)
// controlToPopula te.add(myEle) ;
controlToPopula te.appendChild( myEle)
}
}
}
// -->
</script>
</HEAD>
<BODY>

Hope this helps,
- Peter Schmalfeldt

<form name=form>
<table align="center">
<tr>
<td>
<select id="firstChoice " name="firstChoi ce"
onchange="selec tChange(this, form.secondChoi ce, arrItems1,
arrItemsGrp1);" >
<option value="0" selected>[SELECT]</option>
<option value="1">Land</option>
<option value="2">Sea</option>
<option value="3">Air</option>
</select>
</td><td>
<select id="secondChoic e" name="secondCho ice"
onchange="selec tChange(this, form.thirdChoic e, arrItems2,
arrItemsGrp2);" >
</select>
<select id="thirdChoice " name="thirdChoi ce">
</select>
</td>
</tr>
</table>
</form>
</BODY>

gk wrote:
i want to have 2 dpendent drown box. one for country , and the other
for city.

if you change country in box1 , the city list will also change in
box2..
How to do it in javascript ?

can you provide an example with few dumy country name and their cities.

Thank you


Mar 1 '06 #2
gk wrote:
i want to have 2 dpendent drown box. one for country , and the other
for city.
How to do it in javascript ?


Many examples of such functionality exist. Search google for "dependent
select boxes" or "dynamic option lists".
One such solution is mine at
http://www.javascripttoolbox.com/lib/dynamicoptionlist/
Good luck,

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Mar 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
9575
by: lorirobn | last post by:
Hi, I have a form with several combo boxes, continuous form format, with record source a query off an Item Table. The fields are Category, Subcategory, and Color. I am displaying descriptions, not ID's, so I have to convert the ID's from various lookup tables. The combo boxes work fine except for subcategory, which is dependent on category. Depending on category, the drop-down box for subcategory will display different items. (for...
10
22960
by: somaskarthic | last post by:
Hi In my php page , there is a user registration form. Here the user has to select the country, state, city from the drop down box. How this can be handled in php? If a country is selected in a drop down box , its corresponding states should be populated in the state drop down box. If a state is selected , its corresponding cities should be populated in the city drop down box. Where all these data has to be stored and retrieved. What...
5
18725
by: ashok893 | last post by:
I'm using two drop down list ina form. I have generated the first drop down list from MySQL database. When i select an option from first drop down list, i have to generate second drop down list options by the selected value of first drop down list from MySQL database. For example, the first drop down list contains Animals, Birds... If i select the Animal option, the second drop down list should show like Lion, Tiger.... Both lists should...
5
9423
by: jakas | last post by:
Hi In my php page , there is a user registration form. Here the user has to select the country, state, city from the drop down box. How this can be handled in php? If a country is selected in a drop down box , its corresponding states should be populated in the state drop down box. If a state is selected , its corresponding cities should be populated in the city drop down box. Where all these data has to be stored and retrieved. What type...
48
2415
by: coool | last post by:
Hi I'm now trying to have a dependent lists in a form my form is a query based form i.e. I fill my MySQL query from this form I have around 30 fields/columns
1
2605
by: jdkhan | last post by:
i want to use two lists one dependent on other just like city dependent on country selection how can i do this in single page using PHP
2
11104
by: doc1355 | last post by:
Hi, I have two dynamic drop down lists: State and City. What I'm trying to do is first I need the city list to be empty on load. Then after a visitors selects the "state" from the first list, the page reloads and it shows all the available cities from my database where state is selected. Here is what I have sofar: <?php require_once('Connections/myConnection.php'); ?> <?php // Check for errors error_reporting(E_ALL);
4
2189
by: deanndra | last post by:
First, I want to say thank you to Scott and the others who replied to my first post here. I had to put that database on hold for the moment when I was tasked with a new one. I am building another database from scratch. This one is for job announcements. I've built only 2 tables (I know this is a no-no, but it was demanded by those wanting this database so I've complied). The field name properties and data types in both tables are virtually...
0
9387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.