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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Undefined Variable

Status
Not open for further replies.

timmbo

Programmer
Feb 22, 2001
167
US
Hi All,

I'm attempting to retrieve month name from getMonths. My procedure enters getMoths() OK but my alert msg states my variables are undefined. Once I have month name, I need to pass it to my webpage. If anyone knows of an easier way of doing this, I'm interested for any suggestions.

Any help would be appreciated...

JavaScript code follows:

function setMonths(_form) {

var currDate = new Date();
var twoPriorMonths = currDate.getMonth()-1;
var myTwoMonths = getMonths(twoPriorMonths);
alert("myTwoMonths... " + myTwoMonths);
alert("twoPriorMonths... " + myTwoMonths);
}

function getMonths(_month) {

alert("In getMonths...");

switch(_month) {
case("1"):
var myMonth = "January";
break;
case("2"):
var myMonth = "February";
break;
case("3"):
var myMonth = "March";
break;
case("4"):
var myMonth = "April";
break;
case("5"):
var myMonth = "May";
break;
case("6"):
alert("HERE 1");
var myMonth = "June";
alert("In June... " + myMonth);
break;
case("7"):
var myMonth = "July";
break;
case("8"):
var myMonth = "August";
break;
case("9"):
var myMonth = "September";
break;
case("10"):
var myMonth = "October";
break;
case("11"):
var myMonth = "November";
break;
case("12"):
var myMonth = "December";
break;
}
}
 
Change each case as such:

Code:
case("1"):

to

case 1:

Javascript sees "1" as a string (possibly with a value equal to the unicode value of the character "1"). However, 1 (without the quotes) is 1.

'hope this helps.

--Dave
 
use an array:

var tMonth = new Array("January", "February",....);
var currDate = new Date();
var twoPriorMonths = currDate.getMonth()-1;
var myTwoMonths = tMonth[twoPriorMonths];

 
Thanks for the quick response and suggestions.
 
The Array idea worked great ConeHead! Thanks againg...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top