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.


Recommended Answers

All 5 Replies

What have you attached???

hi Dani

i have updated the post. please check the link

Your problem is simple... if(document.frmEntry.optMethod != "undefined") this is testing if an object, that may not exist (undefined) is different than an string with value "undefined".

Two ways to resolve it:

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


if(document.frmEntry.optMethod != undefined )

Or, simplest of all :

if( typeof document.frmEntry.optMethod ) {
commented: Didn't know this one ^^ +0

Sorry, that shoud read :

if( document.frmEntry.optMethod ) {
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.