views:

68

answers:

2

I am working on a jquery code that has a form

  • country dropdown list
  • USA state dropdown list
  • non-usa state text input box

The goal is to have a country dropdown,

If a user selects USA then it will show the USA state list and hide the other state text input.

If a user selects a country other then the USA then it should HIDE the USA state list and show the state input text box

This almost works correctly with my code below except there is a problem.

My code will hide the USA state dropdown list if you select a non-usa country. However if you select the USA, it will show the state dropdown list which is great but it fails to hide the state text input box. I cannot figure out why, I have posted a full example code working so you cann see the problem live if you care to look it is located here http://jsbin.com/alige

Any help would be greatly appreciated.

PS) The reason for needing this extra code with the trigger change is because sometimes a user will already have a country/state saved into a database and when they load the page a country and state will be pre-selected so that bit of code makes sure it does not require a change/click to make it update on page load.

Here is some code.

the 3 form items

<select name="country" id="country" class="textarealong  signup_good"/><option value=1001>Choose a Country</option><option value=1002>------------------</option><option value=224 selected="selected">United States</option><option value=39>Canada</option><option value=223>United Kingdom (Great Britain)</option><option value=1000>------------------</option><option value=1>Afghanistan</option><option value=2>Albania</option><option value=3>Algeria</option><option value=4>American Samoa</option><option value=5>Andorra</option><option value=6>Angola</option><option value=7>Anguilla</option><option value=8>Antarctica</option><option value=9>Antigua and Barbuda</option><option value=10>Argentina</option><option value=11>Armenia</option><option value=12>Aruba</option><option value=13>Australia</option><option value=14>Austria</option><option value=15>Azerbaijan</option><option value=16>Bahamas</option><option value=17>Bahrain</option><option value=18>Bangladesh</option><option value=19>Barbados</option><option value=20>Belarus</option><option value=21>Belgium</option><option value=22>Belize</option><option value=23>Benin</option><option value=24>Bermuda</option><option value=25>Bhutan</option><option value=26>Bolivia</option><option value=27>Bosnia and Herzegowina</option><option value=28>Botswana</option><option value=29>Bouvet Island</option><option value=30>Brazil</option><option value=31>British Indian Ocean Territory</option><option value=32>British Virgin Islands</option><option value=33>Brunei Darussalam</option><option value=34>Bulgaria</option><option value=35>Burkina Faso</option><option value=36>Burundi</option><option value=37>Cambodia</option><option value=38>Cameroon</option><option value=40>Cape Verde</option><option value=41>Cayman Islands</option><option value=42>Central African Republic</option><option value=43>Chad</option><option value=44>Chile</option><option value=45>China</option><option value=46>Christmas Island</option><option value=47>Cocos (Keeling) Islands</option><option value=48>Colombia</option><option value=49>Comoros</option><option value=50>Congo</option><option value=51>Cook Islands</option><option value=52>Costa Rica</option><option value=53>Cote D'ivoire</option><option value=54>Croatia</option><option value=55>Cuba</option><option value=56>Cyprus</option><option value=57>Czech Republic</option><option value=58>Czechoslovakia</option><option value=59>Denmark</option><option value=60>Djibouti</option><option value=61>Dominica</option><option value=62>Dominican Republic</option><option value=63>East Timor</option><option value=64>Ecuador</option><option value=65>Egypt</option><option value=66>El Salvador</option><option value=67>Equatorial Guinea</option><option value=68>Eritrea</option><option value=69>Estonia</option><option value=70>Ethiopia</option><option value=71>Falkland Islands (Malvinas)</option><option value=72>Faroe Islands</option><option value=73>Fiji</option><option value=74>Finland</option><option value=75>France</option><option value=76>France, Metropolitan</option><option value=77>French Guiana</option><option value=78>French Polynesia</option><option value=79>French Southern Territories</option><option value=80>Gabon</option><option value=81>Gambia</option><option value=82>Georgia</option><option value=83>Germany</option><option value=84>Ghana</option><option value=85>Gibraltar</option><option value=86>Greece</option><option value=87>Greenland</option><option value=88>Grenada</option><option value=89>Guadeloupe</option><option value=90>Guam</option><option value=91>Guatemala</option><option value=92>Guinea</option><option value=93>Guinea-Bissau</option><option value=94>Guyana</option><option value=95>Haiti</option><option value=96>Heard and McDonald Islands</option><option value=97>Honduras</option><option value=98>Hong Kong</option><option value=99>Hungary</option><option value=100>Iceland</option><option value=101>India</option><option value=102>Indonesia</option><option value=103>Iraq</option><option value=104>Ireland</option><option value=105>Islamic Republic of Iran</option><option value=106>Israel</option><option value=107>Italy</option><option value=108>Jamaica</option><option value=109>Japan</option><option value=110>Jordan</option><option value=111>Kazakhstan</option><option value=112>Kenya</option><option value=113>Kiribati</option><option value=114>Korea</option><option value=115>Korea, Republic of</option><option value=116>Kuwait</option><option value=117>Kyrgyzstan</option><option value=118>Laos</option><option value=119>Latvia</option><option value=120>Lebanon</option><option value=121>Lesotho</option><option value=122>Liberia</option><option value=123>Libyan Arab Jamahiriya</option><option value=124>Liechtenstein</option><option value=125>Lithuania</option><option value=126>Luxembourg</option><option value=127>Macau</option><option value=128>Macedonia</option><option value=129>Madagascar</option><option value=130>Malawi</option><option value=131>Malaysia</option><option value=132>Maldives</option><option value=133>Mali</option><option value=134>Malta</option><option value=135>Marshall Islands</option><option value=136>Martinique</option><option value=137>Mauritania</option><option value=138>Mauritius</option><option value=139>Mayotte</option><option value=140>Mexico</option><option value=141>Micronesia</option><option value=142>Moldova, Republic of</option><option value=143>Monaco</option><option value=144>Mongolia</option><option value=145>Montserrat</option><option value=146>Morocco</option><option value=147>Mozambique</option><option value=148>Myanmar</option><option value=149>Namibia</option><option value=150>Nauru</option><option value=151>Nepal</option><option value=152>Netherlands</option><option value=153>Netherlands Antilles</option><option value=154>New Caledonia</option><option value=155>New Zealand</option><option value=156>Nicaragua</option><option value=157>Niger</option><option value=158>Nigeria</option><option value=159>Niue</option><option value=160>Norfolk Island</option><option value=161>Northern Mariana Islands</option><option value=162>Norway</option><option value=163>Oman</option><option value=164>Pakistan</option><option value=165>Palau</option><option value=166>Panama</option><option value=167>Papua New Guinea</option><option value=168>Paraguay</option><option value=169>Peru</option><option value=170>Philippines</option><option value=171>Pitcairn</option><option value=172>Poland</option><option value=173>Portugal</option><option value=174>Puerto Rico</option><option value=175>Qatar</option><option value=176>Reunion</option><option value=177>Romania</option><option value=178>Russian Federation</option><option value=179>Rwanda</option><option value=180>Saint Lucia</option><option value=181>Samoa</option><option value=182>San Marino</option><option value=183>Sao Tome and Principe</option><option value=184>Saudi Arabia</option><option value=185>Senegal</option><option value=186>Seychelles</option><option value=187>Sierra Leone</option><option value=188>Singapore</option><option value=189>Slovakia</option><option value=190>Slovenia</option><option value=191>Solomon Islands</option><option value=192>Somalia</option><option value=193>South Africa</option><option value=194>Spain</option><option value=195>Sri Lanka</option><option value=196>St. Helena</option><option value=197>St. Kitts And Nevis</option><option value=198>St. Pierre and Miquelon</option><option value=199>St. Vincent And The Greadines</option><option value=200>Sudan</option><option value=201>Suriname</option><option value=202>Svalbard and Jan Mayen Islands</option><option value=203>Swaziland</option><option value=204>Sweden</option><option value=205>Switzerland</option><option value=206>Syrian Arab Republic</option><option value=207>Taiwan</option><option value=208>Tajikistan</option><option value=209>Tanzania, United Republic of</option><option value=210>Thailand</option><option value=211>Togo</option><option value=212>Tokelau</option><option value=213>Tonga</option><option value=214>Trinidad and Tobago</option><option value=215>Tunisia</option><option value=216>Turkey</option><option value=217>Turkmenistan</option><option value=218>Turks and Caicos Islands</option><option value=219>Tuvalu</option><option value=220>Uganda</option><option value=221>Ukraine</option><option value=222>United Arab Emirates</option><option value=225>United States Virgin Islands</option><option value=226>Uruguay</option><option value=227>Uzbekistan</option><option value=228>Vanuatu</option><option value=229>Vatican City State</option><option value=230>Venezuela</option><option value=231>Viet Nam</option><option value=232>Wallis And Futuna Islands</option><option value=233>Western Sahara</option><option value=234>Yemen</option><option value=235>Yugoslavia</option><option value=236>Zaire</option><option value=237>Zambia</option><option value=238>Zimbabwe</option></select></p>  


<select name="usstate" id="usstate" class="textarealong  signup_good"/ ><option value=1 selected="selected">Alabama</option><option value=2>Alaska</option><option value=52>American Samoa</option><option value=3>Arizona</option><option value=4>Arkansas</option><option value=65>Baker Island</option><option value=5>California</option><option value=6>Colorado</option><option value=7>Connecticut</option><option value=8>Delaware</option><option value=9>District of Col</option><option value=53>Federated State</option><option value=10>Florida</option><option value=11>Georgia</option><option value=54>Guam</option><option value=12>Hawaii</option><option value=66>Howland Island</option><option value=13>Idaho</option><option value=14>Illinois</option><option value=15>Indiana</option><option value=16>Iowa</option><option value=67>Jarvis Island</option><option value=55>Johnston Atoll</option><option value=17>Kansas</option><option value=18>Kentucky</option><option value=68>Kingman Reef</option><option value=19>Louisiana</option><option value=20>Maine</option><option value=56>Marshall Island</option><option value=21>Maryland</option><option value=22>Massachusetts</option><option value=23>Michigan</option><option value=59>Midway Islands</option><option value=24>Minnesota</option><option value=25>Mississippi</option><option value=26>Missouri</option><option value=27>Montana</option><option value=62>Navassa Island</option><option value=28>Nebraska</option><option value=29>Nevada</option><option value=30>New Hampshire</option><option value=31>New Jersey</option><option value=32>New Mexico</option><option value=33>New York</option><option value=34>North Carolina</option><option value=35>North Dakota</option><option value=57>Northern Marian</option><option value=36>Ohio</option><option value=37>Oklahoma</option><option value=38>Oregon</option><option value=58>Palau</option><option value=69>Palmyra Atoll</option><option value=39>Pennsylvania</option><option value=60>Puerto Rico</option><option value=40>Rhode Island</option><option value=41>South Carolina</option><option value=42>South Dakota</option><option value=43>Tennessee</option><option value=44>Texas</option><option value=61>U.S. Minor Outl</option><option value=45>Utah</option><option value=46>Vermont</option><option value=63>Virgin Islands</option><option value=47>Virginia</option><option value=64>Wake Island</option><option value=48>Washington</option><option value=49>West Virginia</option><option value=50>Wisconsin</option><option value=51>Wyoming</option></select> 


<input type="text" id="othstate" name="othstate" id="othstate" value="" class="textarealong  signup_good" maxlength="100">

The jquery javascript

  $(document).ready(function () { 
    $("#otherstate").hide(); 
    $("#country").trigger("change"); 
  }); 

  $("#country").change(function () { 
    if ($("#country").val() != '224') { 
      $("#usstate").hide(); 
      $("#otherstate").show(); 
    } else { 
      $("#usstate").show(); 
      $("#otherstate").hide();     
    } 
  });
+1  A: 

your text input box has id="othstate" but in your code you're referring to it as $('#otherstate')

nickf
+1  A: 

It's because the id for the text box is othstate and in the script you are hiding otherstate.

thorncp
thanks this was driving me nuts +1
jasondavis
In addition to that change I had to move the $("#country").change(function () {... section into the $(document).ready block to get it to work as intended.
Hardwareguy
strange, works for me how it is
thorncp
I will be storing the main code in a seperate file, I would like to wrap it in a function but I can figure out how to make $("#country").trigger("change"); call a function instead, can you help with that?
jasondavis