Condition Always true when checking for "undefined" in Javascript [Resolved]

Posted by Vuyiswamb under JavaScript on 8/3/2014 | Points: 10 | Views : 627 | Status : [Member] [MVP] [Administrator] | Replies : 3
Good Day all

i am reluctantly maintaining a Classic Asp application that will be re-written in few months to come. i have an issue where i have a Code like this

          if(document.frmEntry.optMethod != "undefined ")
{
if (document.frmEntry.optMethod.selectedIndex == 0) {
strPrompt=strPrompt+'Please specify the Valuation Method\n';
if ( objFocus == null ) {
objFocus = document.frmEntry.optMethod;
}
}
}


my problem here is that even if document.frmEntry.optMethod is not equal to "undefined " the code still go into the condition as if it was true. i have attached a proof in my debuger.

http://www.vetauinvest.com/Example/IE_DEBUGGER.png

Thank you for posting at Dotnetfunda
[Administrator]



Responses

Posted by: kgovindarao523-21772 on: 8/4/2014 [Member] [MVP] Bronze | Points: 50

Up
0
Down

Resolved
Hi,
change condition like:

if(typeof document.frmEntry.optMethod != "undefined ")
or
if(! document.frmEntry.optMethod)


Thank you,
Govind

Vuyiswamb, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 8/4/2014 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
That worked well

Thanks

Thank you for posting at Dotnetfunda
[Administrator]

Vuyiswamb, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rasikbapotra on: 8/7/2014 [Member] Starter | Points: 25

Up
0
Down
You can also check for null

if(document.frmEntry.optMethod != null)

Vuyiswamb, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response