Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Javascript] Check if an element exists in the DOM
- From: jdeighan at pcgus.com (John Deighan)
- Subject: [Javascript] Check if an element exists in the DOM
- Date: Wed Oct 18 08:14:45 2006
At 08:49 AM 10/18/2006, you wrote:
>Hello.
>
>How do I check if an element with id="XYZ" is present in my page/DOM ?
>
>I've tried with:
>
>if ($('XYZ') == undefined) {
> // Do something
>} else {
> // Do something else
>}
>
>but, of course, javascript give me an error of undefined object.
The function named '$', I think, is part of a popular library named
prototype.js. You'd have to at a minimum be sure that it's loaded.
The standard DOM way would be:
if (document.getElementById('XYZ')===undef) {
Note that it's better to use '===' to compare to undef since '=='
will to type conversion on it's arguments so that,
e.g. 0==undef and ''==undef will return true, but the same
comparisons using '===' will return false.
- References:
- [Javascript] Check if an element exists in the DOM
- From: Simone Fumagalli
- [Javascript] Check if an element exists in the DOM
- Prev by Date: [Javascript] Check if an element exists in the DOM
- Next by Date: [Javascript] Avoid multiple onclick
- Previous by thread: [Javascript] Check if an element exists in the DOM
- Next by thread: [Javascript] Javascript Tree menu
- Index(es):