/    Sign up×
Community /Pin to ProfileBookmark

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

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?ctober|
November:November|
December?ecember|
‘;

// 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);

}
}

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERAug 22.2009 — From what you have supplied, it is difficult to decide where the problem is.

My first guess is to remove the last | combination in the 'var month' definition.

If that doesn't work, include the HTML that is calling the script

and put you code between the forum tags [ code]...[ /code] (without spaces)

to make the script easier to read.
Copy linkTweet thisAlerts:
@kelsey720authorAug 22.2009 — Thanks for the help. I tried removing the '|' at the end of 'December?ecember|'. It worked when I did that. I don't know why it works but it did. Thanks.
Copy linkTweet thisAlerts:
@JMRKERAug 22.2009 — The reason it worked is because in this line
<i>
</i>var monthLineArray = month.split('|'); // Split into lines

you create an array with elements on either side of each '|' character.

The last element will be '', which is an undefined ID in your code.

Good Luck!

?
×

Success!

Help @kelsey720 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...