Current location: Hot Scripts Forums » General Web Coding » JavaScript » Dynamic dropdown box


Dynamic dropdown box

Reply
  #1 (permalink)  
Old 12-28-09, 11:11 AM
I_have_to_go I_have_to_go is offline
New Member
 
Join Date: Dec 2009
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Dynamic dropdown box

I have two select menus , the second select menu is dependent on the value of the first select menu. I am able to populate the second menu with the correct values but for some reason I cannot restrict which values are shown and when. When the user picks USA, I want the states associated with it to be shown, and when USA is not selected then I don't want those values shown. Here is my code.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="hrg_nonmember_includes/javascript/jquery.js"></script>
<script type="text/javascript">
<!-- <![CDATA[

 

 
$(function() {  




});//closes the big funciton  
// ]]> -->
</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
 
<script type="text/javascript">
<!-- <![CDATA[
var countryState = {
"usa":[{'al':'Alabama',
        'ak':'Alaska', 
		'as':'American Samoa', 
		'az':'Arizona', 
		'ar':'Arkansas',
		'ca':'California',
		'co':'Colorado',
		'ct':'Connecticut',
		'de':'Delaware',
		'dc':'District Of Columbia',
		'fm':'Federated States Of Micronesia',
		'fl':'Florida',
		'ga':'Georgia',
		'gu':'Guam',
		'hi':'Hawaii',
		'id':'Idaho',
		'il':'Illinois',
		'in':'Indiana',
		'ia':'Iowa',
		'ka':'Kansas',
		'ky':'Kentucky',
		'la':'Louisiana',
		'me':'Maine',
		'mh':'Marshall Islands',
		'md':'Maryland',
		'ma':'Massachusetts',
		'mi':'Michigan',
		'mn':'Minnesota',
		'ms':'Mississippi',
		'mo':'Missouri',
		'mt':'Montana',
		'ne':'Nebraska',
		'nv':'Nevada',
		'nh':'New Hampshire',
		'nj':'New Jersey',
		'nm':'New Mexico',
		'ny':'New York',
		'nc':'North Carolina',
		'nd':'North Dakota',
		'mp':'Northern Mariana Islands',
		'oh':'Ohio',
		'ok':'Oklahoma',
		'or':'Oregon',
		'pw':'Palau',
		'pa':'Pennsylvania',
		'ri':'Rhode Island',
		'sc':'South Carolina',
		'sd':'South Dakota',
		'tn':'Tennessee',
		'tx':'Texas',
		'ut':'Utah',
		'vt':'Vermont',
		'vi':'Virgin Islands',
		'va':'Virginia',
		'wa':'Washington',
		'wv':'West Virginia',
		'wi':'Wisconsin',
		'wy':'Wyoming'

}],//closes usa
"can":[{
'ab':'Alberta',
'bc':'British Columbia',
'mb':'Manitoba',
'nb':'New Brunswick',
'nl':'Newfoundland And Labrador',
'ns':'Nova Scotia',
'nu':'Nunavut',
'nt':'Northwest Territories',
'on':'Ontario',
'pe':'Prince Edward Island',
'qc':'Quebec',
'sk':'Saskatchewan',
'yt':'Yukon'
}],//closes canada
"eu":[{
'at':'Austria',
'be':'Belgium',
'bg':'Bulgaria',
'cy':'Cyprus',
'cz':'Czech Republic',
'dk':'Denmark',
'ee':'Estonia',
'fi':'Finland',
'fr':'France',
'de':'Germany',
'el':'Greece',
'hu':'Hungary',
'ie':'Ireland',
'it':'Italy',
'lv':'Latvia',
'lt':'Lithuania',
'lu':'Luxembourg',
'mt':'Malta',
'nl':'Netherlands',
'pl':'Poland',
'pt':'Portugal',
'ro':'Romania',
'sk':'Slovakia',
'si':'Slovenia',
'es':'Spain',
'se':'Sweden',
'uk':'United Kingdom'

}],//closes european union
"aust":[{
'act':'Australian Capital Territory',
'chr':'Christmas Island',
'coc':'Cocos Island',
'jb':'Jervis Bay',
'nsw':'New South Wales',
'nt':'Northern Territory',
'qld':'Queensland',
'sa':'South Australia',
'tas':'Tasmania',
'vic':'Victoria',
'wa':'Western Australia'
}]//closes australia

};//closes countryState

/*function countryStatus(){
var check =document.getElementById("country_id"); 

if( check.value == "default_country"){
  document.getElementById("state_id").disabled=true;//alert(check);
}else{document.getElementById("state_id").disabled=false;

}

}closes checkStatus()*/


function stateValue(){
var country = document.getElementById("country_id").value;
var state= document.getElementById("state_id");
var  opt;
var i=0;
var x=0;
var y=0;

state.disabled = true;

if(country != "default_country"){
state.disabled = false;
  for(i in countryState[country]){
     for(x in countryState[country][i]){
      opt = document.createElement('option');
	  opt.value=[x];
	  opt.text=countryState[country][i][x];
	  state.add(opt,null);
	  
	  }
state.remove(opt);
}
}else{state.disabled = true;}

return;
}//closes stateValue()



// ]]> -->
</script>
<!-- -->
<div id="contact_form">  
<form name="contact" action=""> 
<select id="country_id" name="country_id" onblur="stateValue()">
<option selected="selected" value="default_country">Your Country</option>
<option value="usa">USA</option>
<option value="can">Canada</option>
<option value="eu">European Union</option>
<option value="aust">Australia</option>
</select>


<select id="state_id" name="state_id">
<option value="default_state" selected="selected">Select State</option>
</select>

</form>
</div>   
<script type="text/javascript">
<!-- <![CDATA[



// ]]> -->
</script>
</body>
</html>
Reply With Quote
  #2 (permalink)  
Old 12-28-09, 12:52 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
It might be simpler to have a select tag for each of the different countries and just show/hide them based on the state of the first select tag.

My recommendation would be to go visit some sites and see different approaches, since there are lots of ways to do this - and you can pick the one you like best.

There may be some examples up at jQuery.
Reply With Quote
Reply

Bookmarks


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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Populate dropdown box with records from database tspandiel Script Requests 3 07-03-07 03:01 PM
copy a part of a dropdown box selection to a textbox anarchoi JavaScript 1 05-07-07 03:23 AM
DropDown Box Poll system espressokid Script Requests 1 03-06-05 08:06 PM
dynamic combo box jaishalg PHP 1 02-18-05 09:37 AM
Dropdown box for locating files gd12 PHP 0 12-18-04 09:11 PM


All times are GMT -5. The time now is 01:59 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.