Check for document ID?

M

michael

Is there a way for Javascript to check if a document CSS id exist anywhere
on a page, for example, if "bla" exists?

<a href="asf" id="bla">
 
C

CryingClinton

var obj=document.getElementById('bla');
if (typeof(obj)=='undefined' ) alert('The object does not exist!'); // or
typeof(obj)!='object'
 
R

Richard Cornford

CryingClinton said:
var obj=document.getElementById('bla');
if (typeof(obj)=='undefined' ) alert('The object does not exist!');
// or typeof(obj)!='object'

The W3C Core DOM - getElementById - method is specified as returning -
null - when it cannot locate an element with a corresponding ID
attribute in the DOM. The - typeof - operator returns the string
"object" when its operand is - null - so neither of these tests will
work. A type-converting test would be more effective in this situation.

Please do not top-post in comp.lang.javascirpt.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top