473,799 Members | 3,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use multiple state drop downs from one country list

3 New Member
Hi, I plan to use drop down lists to populate team results and will use the standard country/state drop down lists as the base code.

The question: how can I populate multiple (5) states from entering one country?

Here is the current code which works for one state...however if I add another "State" (class=td100) row identical to the one below neither state field offers the drop down list.

Expand|Select|Wrap|Line Numbers
  1. <form name="form1" action=""> 
  2. <table style="width:590px;"><tbody><tr>
  3. <td style="width:100px;">Country</td>
  4. <td><select name="cboCountry" style="width:auto;clear:none;" onchange="Fill_States();">
  5. <option selected="selected">12345678901234567890</option>
  6. <option>temp</option></select></td>
  7. </tr>
  8.  
  9. <tr>
  10.  
  11. <td class="td100">State<br />Province</td>
  12. <td title="Enable javascript to fill the lists."><select name="cboState" style="width:auto;" onchange="Update_Globals();">
  13.  
  14. <option selected="selected">12345678901234567890</option>
  15. <option>temp</option></select></td></tr>
  16. </tbody></table>
  17.  
Many thanks
Barry
Aug 6 '09 #1
5 3941
gits
5,390 Recognized Expert Moderator Expert
as far as i understand that ... this is a JavaScript issue ... since you cannot achieve that just with html ... to help you with this, you should explain a bit more what you mean with 'standard country/state drop down lists' and how the code currently populates the one list that is filled correctly. may be posting some code regarding that would help a lot.

kind regards
Aug 9 '09 #2
bennever
3 New Member
Thanks. Here is a cut-down version of the code...

Expand|Select|Wrap|Line Numbers
  1. var postState = '';
  2. var postCountry = '';
  3.  
  4.  
  5. var state = '\
  6. US:AK:Alaska|\
  7. US:AL:Alabama|\
  8. etc
  9. etc
  10. ';
  11.  
  12. // Country data table
  13. //
  14. // To edit the list, just delete a line or add a line. Order is important.
  15. // The order displayed here is the order it appears on the drop down.
  16. //
  17. var country = '\
  18. AF:Afghanistan|\
  19. AL:Albania|\
  20. etc
  21. etc
  22. ';
  23.  
  24. function TrimString(sInString) {
  25.   if ( sInString ) {
  26.     sInString = sInString.replace( /^\s+/g, "" );// strip leading
  27.     return sInString.replace( /\s+$/g, "" );// strip trailing
  28.   }
  29. }
  30.  
  31. // Populates the country selected with the counties from the country list
  32. function populateCountry(defaultCountry) {
  33.   if ( postCountry != '' ) {
  34.     defaultCountry = postCountry;
  35.   }
  36.   var countryLineArray = country.split('|');  // Split into lines
  37.   var selObj = document.getElementById('countrySelect');
  38.   selObj.options[0] = new Option('Select Country','');
  39.   selObj.selectedIndex = 0;
  40.   for (var loop = 0; loop < countryLineArray.length; loop++) {
  41.     lineArray = countryLineArray[loop].split(':');
  42.     countryCode  = TrimString(lineArray[0]);
  43.     countryName  = TrimString(lineArray[1]);
  44.     if ( countryCode != '' ) {
  45.       selObj.options[loop + 1] = new Option(countryName, countryCode);
  46.     }
  47.     if ( defaultCountry == countryCode ) {
  48.       selObj.selectedIndex = loop + 1;
  49.     }
  50.   }
  51. }
  52.  
  53. function populateState() {
  54.   var selObj = document.getElementById('stateSelect');
  55.   var foundState = false;
  56.   // Empty options just in case new drop down is shorter
  57.   if ( selObj.type == 'select-one' ) {
  58.     for (var i = 0; i < selObj.options.length; i++) {
  59.       selObj.options[i] = null;
  60.     }
  61.     selObj.options.length=null;
  62.     selObj.options[0] = new Option('Select State','');
  63.     selObj.selectedIndex = 0;
  64.   }
  65.   // Populate the drop down with states from the selected country
  66.   var stateLineArray = state.split("|");  // Split into lines
  67.   var optionCntr = 1;
  68.   for (var loop = 0; loop < stateLineArray.length; loop++) {
  69.     lineArray = stateLineArray[loop].split(":");
  70.     countryCode  = TrimString(lineArray[0]);
  71.     stateCode    = TrimString(lineArray[1]);
  72.     stateName    = TrimString(lineArray[2]);
  73.   if (document.getElementById('countrySelect').value == countryCode && countryCode != '' ) {
  74.     // If it's a input element, change it to a select
  75.       if ( selObj.type == 'text' ) {
  76.         parentObj = document.getElementById('stateSelect').parentNode;
  77.         parentObj.removeChild(selObj);
  78.         var inputSel = document.createElement("SELECT");
  79.         inputSel.setAttribute("name","state");
  80.         inputSel.setAttribute("id","stateSelect");
  81.         parentObj.appendChild(inputSel) ;
  82.         selObj = document.getElementById('stateSelect');
  83.         selObj.options[0] = new Option('Select State','');
  84.         selObj.selectedIndex = 0;
  85.       }
  86.       if ( stateCode != '' ) {
  87.         selObj.options[optionCntr] = new Option(stateName, stateCode);
  88.       }
  89.       // See if it's selected from a previous post
  90.       if ( stateCode == postState && countryCode == postCountry ) {
  91.         selObj.selectedIndex = optionCntr;
  92.       }
  93.       foundState = true;
  94.       optionCntr++
  95.     }
  96.   }
  97.   // If the country has no states, change the select to a text box
  98.   if ( ! foundState ) {
  99.     parentObj = document.getElementById('stateSelect').parentNode;
  100.     parentObj.removeChild(selObj);
  101.   // Create the Input Field
  102.     var inputEl = document.createElement("INPUT");
  103.     inputEl.setAttribute("id", "stateSelect");
  104.     inputEl.setAttribute("type", "text");
  105.     inputEl.setAttribute("name", "state");
  106.     inputEl.setAttribute("size", 20);
  107.     inputEl.setAttribute("value", postState);
  108.     parentObj.appendChild(inputEl) ;
  109.   }
  110. }
  111.  
  112. function initCountry(country) {
  113.   populateCountry(country);
  114.   populateState();
  115. }
  116. //-->
  117. </script>
  118.  
  119.  
  120.  
  121. // End -->
Aug 9 '09 #3
gits
5,390 Recognized Expert Moderator Expert
as you could see from your posted code ... you could only populate ONE list since you have hardcoded ids that are use like this:

Expand|Select|Wrap|Line Numbers
  1. document.getElementById('stateSelect');
so to poulate more then one node you could pass an additional id or id-list to the functions and use that, so you could either make a number of calls with one id or write a loop inside the function so that you could pass a list ...

kind regards
Aug 10 '09 #4
bennever
3 New Member
Thanks for your help on this. I'm fine with html/css, even php, but js is sadly beyond me at the moment. I found this free drop down coding and thought it would be ideal for snooker league teams to send in their results ie team name is selected and then player names are populated (as states are in the example).

If it's not too much trouble would you be able to give me the changes I would need to make to the code to populate 5 nodes from one drop down selection? Kind rgds

Barry
Aug 10 '09 #5
gits
5,390 Recognized Expert Moderator Expert
?? ... i will not give you the entire solution for this ... because this is not what i'm here for. in case you show some efforts to learn and try something for your own i'm glad to help you out ...and point you to the right direction, the idea for the adaptions is up there in my previous post ... :)
Aug 12 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
7802
by: B. G. Mahesh | last post by:
hi In the registration form I have city, state, country fields. I was wondering if there was a database available on the net which has the list of states in each of the countries. That way when a user selects a country I could automatically populate the state drop down menu Any help is appreciated -- bg mahesh
46
5148
by: Kingdom | last post by:
In my data base I have a list of componet types e.g. type A - I have 8 off - type B I have 12 off etc. I'm using Set objRS = objDC.Execute("Select DISTINCT Component_Type FROM Parts_Table") to populate a drop down but would like to use several drop downs restricting the contents of each drop down to the records pertaining to one
1
3509
by: Aaron Prohaska | last post by:
I'm having the problem with this drop down list on postback. For some reason both the ListItems get selected when I change the selected item. Using the code below I'm building the drop down list in the overriden CreateChildControls method and setting the selected item. Then when I change the item in the drop down list the list is rebuilt from viewstate, but the initial item is still selected causing the error below. I also have a number...
10
22961
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...
1
3594
by: Jeff Gardner | last post by:
Greetings: I have a table with 3 pieces of data that I would like to use to dynamically populate 3 drop downs using javascript. The fields are state, orgname, office. If it's not already obvious, I'd like orgname drop down to change when a state is selected and I would like office drop down to change when an orgname is selected. I can do this with multiple tables but am having difficulty getting it to work when the data is in the...
1
1789
by: Mpho Pole | last post by:
Hi everyone.I'm a newly minted member, and I'm glad to be here. Now that the pleasanteries are out of the way, my problem is as follows: I have an SQL query which receives multiple parameters from a form depending on the users' choice. Most of the parameters are in drop down combos on the said form and I would like my SQL statement to return results according to the values of the drop downs regardless of the number of drop downs. The drop...
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...
0
2254
by: kishorealla | last post by:
Hello I need to create a web bot/crawler/spider that would go into different web sites and collect data for us and store in a database. The crawler needs to 'READ' the options on a website (either from drop-downs, radio-buttons or check-boxesand) to create some input itself OR use some generic pre-defined words (that we provide it with). For example, a webpage might be structure with a text field and some drop-downs. Typically, if the user...
7
9246
by: tokcy | last post by:
Hi everyone, I need the world database of country state and city. Actually i have three drop down option in my project in 1st drop down country name should come from database and 2nd drop down state name should come from database according to country and in 3rd drop down cities name should come from database according to states. I have tried many search engine but i could not get any proper info regarding this i mean some time i get whole...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10249
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...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10025
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
9068
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...
0
6804
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4138
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
3
2937
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.