Solved

NaN error on subtraction of selectedIndex

Posted on 2006-07-13
10
374 Views
Last Modified: 2008-02-26
Why would the below code return NaN:

alert("DOES NOT WORK - RETURNS NaN:"+document.pollform.month[document.pollform.month.selectedIndex].value - 1);

if I remove the subtraction it works fine???

alert("WORKS:"+document.pollform.month[document.pollform.month.selectedIndex].value - 1);
0
Comment
Question by:894359
10 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 250 total points
ID: 17097322
Use:

alert("DOES NOT WORK - RETURNS NaN:"+(document.pollform.month[document.pollform.month.selectedIndex].value - 1));

-r-
0
 

Author Comment

by:894359
ID: 17097356
This is what is actually failing:

age.setMonth(document.pollform.month[document.pollform.month.selectedIndex].value-1);
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17097363
Check this:

    alert("WORKS:"+(document.pollform.month.value-1))


One question for explanation: does JavaScript evaluate the statement expression from left to right or right to left?
And if it is from left to right, what number should yield this:  ""DOES NOT WORK - RETURNS NaN: 9"-1;

0
 
LVL 2

Expert Comment

by:vikrant4u2
ID: 17097366
try using the way roonaan has suggested.

i think alert tries to evaluate the msg passed to it.

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17097372
Your var age is NOT an Object of type: Date
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:894359
ID: 17097377
It is I just didn't post the whole code!

var age = new Date();
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 250 total points
ID: 17097404
If one of the names, the form or the select, is wrong spelled, like it Month, or PollForm, then you get "undefined": document.pollform.month
Check twice the month select name. Is it perhaps "months"?


0
 

Author Comment

by:894359
ID: 17097443
I found the issue. it was on the getting of the dates

age.getMonth vs age.getMonth()



0
 

Author Comment

by:894359
ID: 17097473
I should have stated my question better! :(
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17098003
You can ask for delete for this question here: http:/Community_Support/askQuestion.jsp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now