The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Checking if an ID exists

I've seen numerous examples that seem to be checking to see if an ID exists in the HTML. All these examples utilize "typeof". I can't get it to work that way. I've found that the "typeof" test always passes, and in IE6, a reference to the undefined ID then results in a javascript error.
For example, if an ID of "hello" is defined in the HTML, then I want to set the color, and if it is not defined, then do nothing:var testid = "hello";var isDOM = 1; // assume a previous test set this variablevar myElement;if(isDOM && typeof document.getElementById(testid)!="undefined"){
myElement=document.getElementById(testid);
myElement.style.color="green";}
If an ID of "hello" does not exist in the HTML code, then this javascript will fail with an error on the line that attempts to set the color. The "typeof" test seems to always return a value of "object".

But trial and error produced this method which seems to work fine in IE6, NS7, and Opera7:var myElement;if(isDOM &&(myElement=document.getElementById(testid))){
myElement.style.color="green";}
Is there a proper way to test if an ID exists?
Is my method safe to use in a DOM-compliant browser?

Thanks for the helpful info on the use of "null". The short-circuit check you suggest does not work properly as written. It always seems to report "true". The extra pair of parends in the example below seems to adjust the precedence so that it works properly.

The problem with short-circuits like that isn't that they don't work, it's that other developers who read the code (especially any who routinely work in other programming languages where a simple "=" changes meaning depending on context) might mis-understand what's actually happening, and assume that the above is an equality comparison between myElement and document.getElementById(sPage).

If you can be sure that you're the only developer that will ever work on this code, and that when you revisit this code in 18 months' time, you will quickly recognise what's REALLY happening, then go right ahead. But if I were code-reviewing work for checkin to a corporate codebase maintained by multiple programmers, where there is a realistic chance of developers leaving the project at some point in the next decade, I'd require a change to the more straightforward and obvious:

For the sake of a percieved "optimisation", that will probably have minimal (if any) effect on performance, and only a trivial effect on code size, you are creating a fairly serious misunderstanding-waiting-to-happen for future code maintenance. People can spend DAYS chasing shadows of bugs because they don't spot stuff like this.