Go Back   SitePoint Forums > Forum Index > Program Your Site > JavaScript
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Apr 11, 2006, 07:18   #1
Ize
SitePoint Guru
 
Ize's Avatar
 
Join Date: Nov 2005
Location: The Netherlands
Posts: 808
getElementById : IE gives warning when element with ID is not present

During practically every project I do I'm facing the same problem at some point.

Picture this, I need to fetch an element with where "foobar" is its ID, to change its style:

Code:
var el = document.getElementById('foobar');
el.style.display = 'block';
No problem here.
But say, my page is a dynamic one, fetching content based on GET-variables. Say my foobar-element disappears from the document, due to GET-variables being set differently.
Of course, Javascript can't find the element. Firefox handles this pretty well by failing silently in the background. IE on the other hand, shows an ugly warning message to my poor users, that el doesn't has a style-property!

The workaround:

Code:
if (document.getElementById('foobar') {
  var el = document.getElementById('foobar');
  el.style.display = 'block';
}
But this is just stupid. What if I want to fetch a hundred elements? Plus it doesn't look or read nice.

Is there a quick solution to this problem?
Ize is offline   Reply With Quote
Old Apr 11, 2006, 08:44   #2
AutisticCuckoo
SitePoint Wizard
silver trophybronze trophy
 
Join Date: Nov 2004
Location: Åsnorrbodarna
Posts: 11,777
I don't think that's stupid (except for the typo ).
It would be much more stupid to try to assign a property to something that is undefined.

In any professional development environment, you need to do error checking. Verify that the object exists before trying to do anything with it.

If you do this a lot, why not make a function out of it?
Code:
function displayAsBlock(id)
{
    var  el = document.getElementById(id);
    if (el) el.style.display = "block";
}

displayAsBlock("foobar");
displayAsBlock("snafu");
Cleaner and easier to read IMHO.
AutisticCuckoo is offline   Reply With Quote
Old Apr 11, 2006, 08:49   #3
Ize
SitePoint Guru
 
Ize's Avatar
 
Join Date: Nov 2005
Location: The Netherlands
Posts: 808
Quote:
Originally Posted by AutisticCuckoo
I don't think that's stupid (except for the typo ).
It would be much more stupid to try to assign a property to something that is undefined.

In any professional development environment, you need to do error checking. Verify that the object exists before trying to do anything with it.

If you do this a lot, why not make a function out of it?
Code:
function displayAsBlock(id)
{
    var  el = document.getElementById(id);
    if (el) el.style.display = "block";
}

displayAsBlock("foobar");
displayAsBlock("snafu");
Cleaner and easier to read IMHO.
Of course, I make functions out of most often used actions, but this just... I dunno, it feels like there should be an easier way

And what I actually meant with "too often" was the situation. It's not about the effect, it's about how I get there.
Ize is offline   Reply With Quote
Old Apr 12, 2006, 00:16   #4
AutisticCuckoo
SitePoint Wizard
silver trophybronze trophy
 
Join Date: Nov 2004
Location: Åsnorrbodarna
Posts: 11,777
Quote:
Originally Posted by Ize
I dunno, it feels like there should be an easier way
Code:
try {
    document.getElementById("foobar").style.display = "block";
    document.getElementById("snafu").style.display = "block";
    ...
} catch (e) {
    if (e.name != "TypeError") throw e;
}
AutisticCuckoo is offline   Reply With Quote
Old Apr 12, 2006, 00:29   #5
Ize
SitePoint Guru
 
Ize's Avatar
 
Join Date: Nov 2005
Location: The Netherlands
Posts: 808
Quote:
Originally Posted by AutisticCuckoo
Code:
try {
    document.getElementById("foobar").style.display = "block";
    document.getElementById("snafu").style.display = "block";
    ...
} catch (e) {
    if (e.name != "TypeError") throw e;
}
Sod ya
Ize is offline   Reply With Quote
Old Apr 12, 2006, 00:55   #6
Kravvitz
CSS & JS/DOM Adept
bronze trophy
 
Join Date: Mar 2005
Location: USA
Posts: 5,479
Quote:
Originally Posted by Ize
Of course, Javascript can't find the element. Firefox handles this pretty well by failing silently in the background. IE on the other hand, shows an ugly warning message to my poor users, that el doesn't has a style-property!
I expect that Firefox is generating an error in the JavaScript Console. Firefox just doesn't open the JavaScript Console when an error occurs by default.
Kravvitz is offline   Reply With Quote
Old Apr 12, 2006, 02:07   #7
felgall
Programming Since 1978
silver trophybronze trophy
SitePoint Award Recipient
 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 10,811
Opera usually sticks the error message in the Console and terminates the script at that point and you have to open the console to find the error.

What about this for a shorter version that combines the assignment (hence single equals) into the if test allowing the whole thing to be done on one line:

if (el = document.getElementById('foobar') el.style.display = 'block';
felgall is online now   Reply With Quote
Old Apr 12, 2006, 02:09   #8
Ize
SitePoint Guru
 
Ize's Avatar
 
Join Date: Nov 2005
Location: The Netherlands
Posts: 808
Quote:
Originally Posted by felgall
Opera usually sticks the error message in the Console and terminates the script at that point and you have to open the console to find the error.

What about this for a shorter version that combines the assignment (hence single equals) into the if test allowing the whole thing to be done on one line:

if (el = document.getElementById('foobar') el.style.display = 'block';
Hey that's not bad at all
Nice one, gonna remember this one.
Ize is offline   Reply With Quote
Old Apr 12, 2006, 02:51   #9
dek
SitePoint Addict
 
dek's Avatar
 
Join Date: Oct 2004
Location: UK
Posts: 353
Quote:
Originally Posted by felgall
if (el = document.getElementById('foobar') el.style.display = 'block';
Need... One... More.... Bracket....


Code:
if (el = document.getElementById('foobar')) el.style.display = 'block';
Although personally, I'd still make a function call of it.
dek is offline   Reply With Quote
Old Apr 12, 2006, 02:48   #10
Iain G
d^_^b
 
Iain G's Avatar
 
Join Date: Nov 2005
Location: Gloucester, UK
Posts: 262
If you're using the line:
Code:
var el = document.getElementById('foobar');
-all you need to do to test is:
Code:
if (el) el.style.display = 'block';
Iain G is offline   Reply With Quote
Old Apr 12, 2006, 23:51   #11
felgall
Programming Since 1978
silver trophybronze trophy
SitePoint Award Recipient
 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 10,811
Quote:
Originally Posted by Iain G
If you're using the line:
Code:
var el = document.getElementById('foobar');
-all you need to do to test is:
Code:
if (el) el.style.display = 'block';
That is just the one statement I posted split into two separate statements.
felgall is online now   Reply With Quote
Old Apr 12, 2006, 03:49   #12
stereofrog
SitePoint Wizard
 
stereofrog's Avatar
 
Join Date: Apr 2004
Location: germany
Posts: 4,321
Code:
$ = function (id) {
  return document.getElementById(id);
}
//
$('foobar') && $('foobar').style.display = 'block';
stereofrog is offline   Reply With Quote
Old Apr 13, 2006, 02:20   #13
Iain G
d^_^b
 
Iain G's Avatar
 
Join Date: Nov 2005
Location: Gloucester, UK
Posts: 262
True, but I've never seen your example in the wild except where someone has mistyped their comparison operator as an assignment and browsers will throw warnings about it. The example I used is more usual.
Iain G is offline   Reply With Quote
Old Apr 13, 2006, 04:08   #14
dek
SitePoint Addict
 
dek's Avatar
 
Join Date: Oct 2004
Location: UK
Posts: 353
Felgall's tends to be more popular amongst the higher echelons of JS gurus (in my experience)
I've seen it all over, especially where brevity and elegance of coding are key (Mike Foster's code, for example, is filled with similar instances)
dek is offline   Reply With Quote
Old Apr 13, 2006, 04:12   #15
Iain G
d^_^b
 
Iain G's Avatar
 
Join Date: Nov 2005
Location: Gloucester, UK
Posts: 262
You learn something new every day.
Iain G is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 14:53.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved