Directly reference HTML elements [duplicate]

Being able to select elements on the page based on their [id] is a “feature” from early JavaScript (DOM lvl 0 or 1 methinks, can’t seem to find the source).

Unfortunately it’s a temperamental feature. What would happen if you had:

<div id="window"></div>

or

<div id="document"></div>

Better yet, what happens here?

<div id="foo"></div>
<div id="bar"></div>
<script>var foo = document.getElementById('bar');</script>

So the nuances to calling an element based on it’s [id] is simply this:

Don’t use it.

It’s not consistent, and not guaranteed to receive future support.

Personally I’d like to see this “feature” go the way of document.all. It only causes more issues than it solves, and document.getElementById, while certainly verbose, is meaningful and understandable.

Leave a Comment