Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  139] [ 1]  / answers: 1 / hits: 31712  / 13 Years ago, thu, november 17, 2011, 12:00:00

I have three pages utilizing the same code and on one of the pages this variable doesn't exist, on the other two the variable ticketType has a value of 1 or 2. I need to first check if ticketType exists and isn't undefined and secondly, need to determine if it's one or 2.



This if statement generates an error:



if(typeof ticketType != undefined && ticketType == 1){}


It's saying ticketType isn't defined. I tried nesting the if statements to check if it was defined first thinking it wouldn't go and try the inner if statement but firebug still generates an error.



Any ideas? There has to be a way to do this...


More From » javascript

 Answers
10

'undefined' needs to have quotes around it when used with typeof



if(typeof ticketType != 'undefined' && ticketType == 1){}

[#89056] Wednesday, November 16, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anjelicadixied

Total Points: 742
Total Questions: 94
Total Answers: 97

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
;