Link to home
Start Free TrialLog in
Avatar of MJ
MJFlag for United States of America

asked on

NaN error on subtraction of selectedIndex

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);
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MJ

ASKER

This is what is actually failing:

age.setMonth(document.pollform.month[document.pollform.month.selectedIndex].value-1);
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;

Avatar of vikrant4u2
vikrant4u2

try using the way roonaan has suggested.

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

Your var age is NOT an Object of type: Date
Avatar of MJ

ASKER

It is I just didn't post the whole code!

var age = new Date();
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MJ

ASKER

I found the issue. it was on the getting of the dates

age.getMonth vs age.getMonth()



Avatar of MJ

ASKER

I should have stated my question better! :(
You can ask for delete for this question here: http:/Community_Support/askQuestion.jsp