Solved

Undefined Value in drop down box countries and states

Posted on 2010-11-17
4
391 Views
Last Modified: 2012-06-21
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
Comment
Question by:nismor240sx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
leehanken earned 175 total points
ID: 34160376
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
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 75 total points
ID: 34160395
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
 
LVL 1

Author Closing Comment

by:nismor240sx
ID: 34160496
Thank you.
0
 
LVL 1

Author Comment

by:nismor240sx
ID: 34160500
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SharePoint and CAML query help 4 26
Keyup listener getting fired on initial page load 1 53
Multiple of Image Swaps 5 41
JSON help 8 47
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question