Go Back   kirupaForum > Development > Client-Side (HTML, CSS, JavaScript)

Reply
 
Thread Tools Display Modes
Old 04-21-2008, 12:14 PM   #1
Gixm87
Registered User
jscript 3 layer drop down help plz?

Hello. Ok so heres my problem I Found a javascript with an xml that is a 2 layer country / state selection drop down. I am trying to alter it to be a 3 layer drop down of the same type through xml. Ive searched quite a few times for a 3 layer but i cant find any I have the basis of the 3 layer it just wont work. So could any of you tell me what i should do next. The javascript and the xml is below any tips/help would be GREAT

Code:
var xmlDoc;

// For IE based browsers (tested under IE6 and IE7):
if (window.ActiveXObject) 
{
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
}
// For Mozilla based browsers (Netsacpe/Firefox):
else if (document.implementation && document.implementation.createDocument) 
{
    xmlDoc = document.implementation.createDocument("","doc",null);
}

// Turn off asynchronus download.
// Load the entire file before trying to do anything with it.
xmlDoc.async=false;

//load the country/state XML file
xmlDoc.load("promocats.xml");

// Populate selected dropdown list with data from XML file
// (<option value="County/StateName">County/StateName</option>)
// Function used for both Country Fill and State Fill 
function fillList(selectbox,text,value)
{
    var optionValue = document.createElement("option");
    optionValue.text = text;
    optionValue.value = value;
    selectbox.options.add(optionValue);
}

// Populate Country list on page load
// Requires: <body onload="fillCountryList();"> on HTML page
function fillCountryList ()
{
    // Get country element ID
    var countryList = document.getElementById("cbocategory");

    // Clear any current values in select box
    // If JavaScript isn't working existing text will remain
    for (var x = countryList.options.length-1; x >-1; x--)
    {
        countryList.options[x] = null;
    }
    // Fill Country name Array from XML file
    var countryNames = xmlDoc.getElementsByTagName("category");
    // Gets total Number of countries in XML file
    var numberOfCountries = countryNames.length;

    // Loop through Country name Array and populate country select
    for (var i=0; i<=numberOfCountries-1; i++)
    {
        var currentCountry =  countryNames[i].getAttribute("name");
        fillList(countryList,currentCountry,currentCountry);
    }
}

// Populate State/Province list on Country change
function fillStateList()
{
    // Get State/Province element ID
    var stateList = document.getElementById("cbosubcat")
    
    // Clear any current values in select box
    // If JavaScript isn't working existing text will remain
    for (var x = stateList.options.length-1; x >-1; x--)
    {
        stateList.options[x] = null;
    }
    
    // Get currently selected ID from country element ID
    var countryListSelected = document.getElementById("cbocategory").selectedIndex;
    // Get number of states for current selected Country (populates Array)
    var numberStates = xmlDoc.getElementsByTagName("category")[countryListSelected].getElementsByTagName("subcat").length;
   
   // Loop through States/Province Array and populate State/Province selection for current Country
    for (var i=0; i<=numberStates-1; i++)
    {
        var currentState =  xmlDoc.getElementsByTagName("category")[countryListSelected].getElementsByTagName("subcat")[i].getAttribute("name");
        fillList(stateList,currentState,currentState);
    }
        
}

// Populate State/Province list on Country change
function fillcatList()
{
    // Get State/Province element ID
    var stateList = document.getElementById("cbocat")
    
    // Clear any current values in select box
    // If JavaScript isn't working existing text will remain
    for (var x = stateList.options.length-1; x >-1; x--)
    {
        stateList.options[x] = null;
    }
    
    // Get currently selected ID from country element ID
    var subcatListSelected = document.getElementById("cbosubcat").selectedIndex;
    // Get number of states for current selected Country (populates Array)
    var numberStates = xmlDoc.getElementsByTagName("subcat")[subcatListSelected].getElementsByTagName("cat").length;
   
   // Loop through States/Province Array and populate State/Province selection for current Country
    for (var i=0; i<=numberStates-1; i++)
    {
        var currentState =  xmlDoc.getElementsByTagName("subcat")[subcatListSelected].getElementsByTagName("cat")[i].firstchild.nodeValue;
        fillList(stateList,currentState,currentState);
    }
        
}
Code:
<?xml version="1.0" encoding="utf-8"?>
<categories author="Michael John Grove" title="category, State-Province selections"
date="2008-Feb-05">
  <category name="Boating">
    <subcat name="Boat manufacturers">
    <cat>Flats Boats</cat>
    <cat>Bay Boats</cat>
    <cat>Bass Boats</cat>
    <cat>Open Fishermen</cat>
    <cat>Luxury Sports Fishermen</cat>
    <cat>Kayaks / Canoes</cat>
    <cat>Aluminum Boats</cat>
    <cat>Misc. Boats</cat>
    <cat>Boating associants & Organizations</cat>
    </subcat>
    
    <subcat name="Accessories">
    <cat>Out Boards</cat>
    <cat>Inboards / Sterndrives</cat>
    <cat>Trolling</cat>
    <cat>Other Motors</cat>
    <cat>Boat Motor Accessories</cat>
    <cat>Motor Service / Parts / Manuals / Etc.</cat>
    </subcat>
    
    <subcat name="Other Boating Products">
    <cat>Trailers/Accessories</cat>
    <cat>Custom Fabricators</cat>
    <cat>Cabin/Cockpit</cat>
    <cat>Electronic/Electrical</cat>
    <cat>Seat/Accessories</cat>
    <cat>Maintenance/Care</cat>
    <cat>Safety Equipment</cat>
    <cat>Boat Accessories</cat>
    <cat>Kayak Accessories</cat>
    <cat>Misc. Products</cat>
    <cat>Docks/Lifts</cat>
    <cat>Finance/Insurance</cat>
    <cat>Hardware</cat>
    <cat>Anchors/Chains</cat>
    <cat>Buoys/Fenders</cat>
    </subcat>

  </category>
 
  <category name="Fishing">
     <subcat name="Fresh Water Fishing">
    <cat>Reels</cat>
    <cat>Rods</cat>
    <cat>Lures</cat>
    <cat>Terminal Tackle</cat>
    <cat>Accessories</cat>
    </subcat>
    
    <subcat name="Salt Water Fishing">
    <cat>Reels</cat>
    <cat>Rods</cat>
    <cat>Lures</cat>
    <cat>Terminal Tackle</cat>
    <cat>Accessories</cat>
    </subcat>
    
    <subcat name="Other Fishing Products / Services">
    <cat>Rigged Bait, Scent</cat>
    <cat>Guides / Charters</cat>
    <cat>Destinations</cat>
    <cat>Apparel</cat>
    <cat>Fly Fishing</cat>
    <cat>Carp Fishing</cat>
    <cat>Misc. Fishing</cat>
    <cat>Ice Fishing</cat>
    <cat>Reel/Rod Repair</cat>
    <cat>Conservation Associations</cat>
    <cat>State/Wildlife Agencies</cat>
    <cat>Fishing Forums</cat>
    <cat>Fishing Tournaments</cat>
    </subcat>
</categories>
Gixm87 is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 06-14-2008, 11:08 AM   #2
darwinshome
Registered User
I am sure you have figured this out by now, or have found an alternative solution, but here is the fix for your three-level drop down; for you or anyone else looking:

Javascript, function fillStateList() edited and function fillCityList() added.
Code:
/*
* Author:       Michael John Grove
* Version:      0.1 (02 March 2008)
* Description:  Javascript Country DropDown selection with full Province/State selection
* License:      Creative Commons Attribution-Share Alike 2.5 South Africa License 
*/
// Load XML file before html loads
var xmlDoc;
// For IE based browsers (tested under IE6 and IE7):
if (window.ActiveXObject) 
{
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
}
// For Mozilla based browsers (Netsacpe/Firefox):
else if (document.implementation && document.implementation.createDocument) 
{
    xmlDoc = document.implementation.createDocument("","doc",null);
}
// Turn off asynchronus download.
// Load the entire file before trying to do anything with it.
xmlDoc.async=false;
//load the country/state XML file
xmlDoc.load("3level.xml");
// Populate selected dropdown list with data from XML file
// (<option value="County/StateName">County/StateName</option>)
// Function used for both Country Fill and State Fill 
function fillList(selectbox,text,value)
{
    var optionValue = document.createElement("option");
    optionValue.text = text;
    optionValue.value = value;
    selectbox.options.add(optionValue);
}
// Populate Country list on page load
// Requires: <body onload="fillCountryList();"> on HTML page
function fillCountryList ()
{
    // Get country element ID
    var countryList = document.getElementById("cboCountry");
    // Clear any current values in select box
    // If JavaScript isn't working existing text will remain
    for (var x = countryList.options.length-1; x >-1; x--)
    {
        countryList.options[x] = null;
    }
    // Fill Country name Array from XML file
    var countryNames = xmlDoc.getElementsByTagName("country");
    // Gets total Number of countries in XML file
    var numberOfCountries = countryNames.length;
    // Loop through Country name Array and populate country select
    for (var i=0; i<=numberOfCountries-1; i++)
    {
        var currentCountry =  countryNames.getAttribute("name");
     fillList(countryList,currentCountry,currentCountry);
 }
}
// Populate State/Province list on Country change
function fillStateList()
{
 // Get State/Province element ID
 var stateList = document.getElementById("cboState")
 
 // Clear any current values in select box
 // If JavaScript isn't working existing text will remain
 for (var x = stateList.options.length-1; x >-1; x--)
 {
     stateList.options[x] = null;
 }
 
 // Get currently selected ID from country element ID
 var countryListSelected = document.getElementById("cboCountry").selectedIndex;
 // Get number of states for current selected Country (populates Array)
 var numberStates = xmlDoc.getElementsByTagName("country")[countryListSelected].getElementsByTagName("state").length;
 
// Loop through States/Province Array and populate State/Province selection for current Country
 for (var i=0; i<=numberStates-1; i++)
 {
      var currentState =  xmlDoc.getElementsByTagName("country")[countryListSelected].getElementsByTagName("state").getAttribute("name");
     fillList(stateList,currentState,currentState);
 }
 
}
function fillCityList()
{
 var CityList = document.getElementById("cboCity")
 
     for (var x = CityList.options.length-1; x >-1; x--)
 {
     CityList.options[x] = null;
 }
 var countryListSelected = document.getElementById("cboCountry").selectedIndex;
 var StateListSelected = document.getElementById("cboState").selectedIndex;
 var numberCities = xmlDoc.getElementsByTagName("country")[countryListSelected].getElementsByTagName("state")[StateListSelected].getElementsByTagName("city").length;
 
     for (var i=0; i<=numberCities-1; i++)
 {
       var currentCity =  xmlDoc.getElementsByTagName("country")[countryListSelected].getElementsByTagName("state")[StateListSelected].getElementsByTagName("city").firstChild.nodeValue;
     fillList(CityList,currentCity,currentCity);
 }
}
HTML
HTML Code:
<body onload="fillCountryList();">
<h1>Country slection - with State/Province select</h1>
<p>
   <select id="cboCountry" onchange="fillStateList();">
        <option value="">Select a Country</option>
   </select>
</p>
<p>
<select id="cboState" onchange="fillCityList();">
        <option value="">Select a State</option>
</select>
</p>
<p>
    <select id="cboCity">
        <option value="">Select a City</option>
    </select>
</p>
</body>
XML
Code:
<?xml version="1.0" encoding="utf-8"?>
<countries author="Michael John Grove" title="Country, State-Province, City selections"
date="2008-Feb-05">
<country name="USA">
 <state name="NY">
     <city>New York</city>
     <city>New Jersey</city>    
 </state>
 <state name="California">
     <city>Sanfransico</city>
     <city>Hollywood</city>    
 </state>
</country>
<country name="South Africa">
 <state name="Gauteng">
     <city>Johannesburg</city>
     <city>Pretoria</city>    
 </state>
 <state name="Kwazulu Natal">
     <city>Durban</city>
     <city>Pietermaritsburg</city>    
 </state>
</country>
</countries>
Hope this helps, feel free to ask for assistance if you need it, and please PM me if you find any bugs, I slapped this version together rather quickly.

Kind Regards
Michael
The author of the original country script you used

Edit: fixed small bug in script that displayed the wrong cities.

Last edited by darwinshome; 06-14-2008 at 01:07 PM..
darwinshome is offline   Reply With Quote
Old 07-04-2008, 06:55 AM   #3
rikmg67
Registered User
This is great could this be modified for a search function? would it be as easy as putting it into a form field and querying the db?

Last edited by rikmg67; 07-04-2008 at 07:09 AM..
rikmg67 is offline   Reply With Quote
Old 03-12-2009, 08:10 PM   #4
costacurt
Registered User
Hello darwinshome,
unfortunately your script does not work..

Do you have any suggestions?

Actually I personelly need 3 drop-down menus and lastly to display a result

For example:

1) State

2) City

3) Town

then display: Your Zip Code is: XXXXX

I just made this up but I am about to freak with this code

any help appreciated!
costacurt is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 -4. The time now is 06:42 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com