Martin Honnen <Ma***********@t-online.de> wrote in message news:<3f********@olaf.komtel.net>...
John Ramsden wrote:
... when the id 'junk' doesn't exist anywhere in the document,
instead of returning 'object'?!
[...]
I need this feature of getElementById(), or an equivalent one
(using sound and standard Javascript), in order to know if an
HTML element is defined in the document, and this bug/feature
is driving me nuts: Whatever string I supply to getElementById()
the result is always 'object' whether the string exists or not
as an ID.
document.getElementById returns null if an element of the id passed is
not found, you can for instance script
var element = document.getElementById('whatever');
if (element) {
... // use element
}
Thanks for the reply Martin. I have seen the method you suggest used
in other Javascript examples. But a couple of references I've come
across on-line claim it isn't really kosher, as several values such
as 0 or '' etc all evaluate to false.
I'd really prefer to do things by the book if possible, especially
being a Javascript novice, and these references definitely say one
is supposed to use typeof() for object existence checking.
But then if IE isn't doing things by the book, and is returning a
value it shouldn't, I guess that means some other approach, such
as "if (element)", must be used.
BTW, I'm not the only person to have this problem. See:
http://forums.devshed.com/archive/1/2002/08/1/40857
Cheers
John Ramsden (jo**********@sagitta-ps.com)