Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"undefined" is appearing in the drop down list when it is not wanted

Status
Not open for further replies.

kelsey720

Technical User
Aug 22, 2009
1
CA
I have made a drop down list that is populated with months. However, I keep getting the word 'undefined' appearing in the drop down list too. Any help is appreciated. Thanks.

// JavaScript Document
var month = '\
January:January|\
February:February|\
March:March|\
April:April|\
May:May|\
June:June|\
July:July|\
August:August|\
September:September|\
October:October|\
November:November|\
December:December|\
';

// Save the month & state field names
var monthFieldCfgArray = document.getElementById('cs_config_month_field').value.split(' ');

// Save the names of the fields that hold the month & state default values
var monthDefaultCfgArray = document.getElementById('cs_config_month_default').value.split(' ');

var defaultMonth = false;

function TrimString(sInString) {

if ( sInString ) {

sInString = sInString.replace( /^\s+/g, "" );// strip leading
return sInString.replace( /\s+$/g, "" );// strip trailing
}
}
// Populates the month select with the months from the month list
//
function populateMonth(idName) {

var monthLineArray = month.split('|'); // Split into lines

var selObj = document.getElementById( idName );

selObj.options[0] = new Option('Month','');
selObj.selectedIndex = 0;

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

lineArray = monthLineArray[loop].split(':');

monthCode = TrimString(lineArray[0]);
monthName = TrimString(lineArray[1]);

if ( monthCode != '' ) {

selObj.options[loop + 1] = new Option(monthName, monthCode);
}

if ( defaultMonth == monthCode ) {

selObj.selectedIndex = loop + 1;
}
}
}
// Initialize the drop downs
//
function initMonth() {

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

monthIdName = monthFieldCfgArray[loop];

// Read the default value hidden fields
defaultMonth = document.getElementById( monthDefaultCfgArray[loop] ).value;

populateMonth( monthIdName);
}
}
 
Try removing the last | character in your data:
Code:
...
November:November|\
December:December';
...
That should sort it out for you!

Cheers,
Jeff

[tt]Visit my blog [!]@[/!] Visit Code Couch [!]@[/!] [/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top