Maniacal Rage


25 Jun

Tip: Avoid a Stupid IE7 getElementById Error

Internet Explorer 7 will error and stop processing javascript if you set a variable to the result of document.getElementById when the variable name is the same as the element’s ID. So this will error:

test = document.getElementById('test');

But this will succeed:

my_test = document.getElementById('test');

Stupid to be sure, but at least you can avoid it easily.

Update: A few people wrote in to tell me this can also be avoided by explicitly declaring variables using the var statement, so:

var test = document.getElementById('test');

Will also work if you want to do that instead.