Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

Undefined Value in drop down box countries and states

I am using this script here and am getting an undefined option on the country drop down as the last option. Does anyone know what would be causing this?

http://javascript.internet.com/forms/country-state-drop-down.html

The same thing happens on my script.. it also happens on the original source link, it is the last country option.
0
nismor240sx
Asked:
nismor240sx
  • 2
2 Solutions
 
leehankenCommented:
There is an error in the javascript file in the populateCountry function. The line which says:

  for (var loop = 0; loop < countryLineArray.length; loop++) {

should say

  for (var loop = 0; loop < countryLineArray.length-1; loop++) {

otherwise the loop goes on too many times and adds an invalid item to the list of countries.

Just insert the -1 and it will work.
// Populates the country selected with the counties from the country list
function populateCountry(defaultCountry) {
  if ( postCountry != '' ) {
    defaultCountry = postCountry;
  }
  var countryLineArray = country.split('|');  // Split into lines
  var selObj = document.getElementById('countrySelect');
  selObj.options[0] = new Option('Select Country','');
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < countryLineArray.length-1; loop++) {
    lineArray = countryLineArray[loop].split(':');
    countryCode  = TrimString(lineArray[0]);
    countryName  = TrimString(lineArray[1]);
    if ( countryCode != '' ) {
      selObj.options[loop + 1] = new Option(countryName, countryCode);
    }
    if ( defaultCountry == countryCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
}

Open in new window

0
 
ZvonkoSystems architectCommented:
Remove the last separator pipe character | from the country data string and the empty Option will go away:

ZM:Zambia|\
ZW:Zimbabwe|\
';

Like this:

ZM:Zambia|\
ZW:Zimbabwe';
 
0
 
nismor240sxAuthor Commented:
Thank you.
0
 
nismor240sxAuthor Commented:
You were both correct, thanks for helping me figure it out. Since my script is dynamic using ASP scripts and a database, the first answer was the easiest one for me to implement.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now