Live data from Hacker News

Named element IDs can be referenced as JavaScript globals

css-tricks.com

11–20 of 113 posts

Re: Named element IDs can be referenced as JavaScript globals

#11
post #10
post #7

This is one of those things that pops up every year or two years. Unfortunately, the person writing about the new discovered weird trick almost always fails to precede the article with a big, red, bold "Please don't ever do this".

and then someone always follows up with "Please don't ever do this", without explaining WHY you should never do this: https://wikipedia.org/wiki/Wikipedia:Chesterton's_fence

It's has been explained enough times. It's just that looking things up for yourself seems to have gone out of fashion.

Re: Named element IDs can be referenced as JavaScript globals

#12
iirc this doesn't work in Firefox? or at least it doesn't work the same way as in Chrome. I developed a tiny home-cooked app[0] that depended on this behavior using desktop Chrome which then broke when I tried to use it on mobile Firefox. I then switched it to using

  document.getElementById
like I should have and everything worked fine. Like others in this thread, I recommend not relying on this behavior.

[0]: https://www.robinsloan.com/notes/home-cooked-app/

Re: Named element IDs can be referenced as JavaScript globals

#13
post #5

Now I'm worried of using IDs and finding issues with globals in JavaScript. Seems to be a curious issue to be debugged.

If you read the article and the spec, you'll see that any explicitly created variables will always take precedence over automatic IDs, so any globals will always override these IDs.

Re: Named element IDs can be referenced as JavaScript globals

#14
post #10

Earlier quoted context omitted.

and then someone always follows up with "Please don't ever do this", without explaining WHY you should never do this: https://wikipedia.org/wiki/Wikipedia:Chesterton's_fence

It's has been explained enough times. It's just that looking things up for yourself seems to have gone out of fashion.

That doesn’t help people who stumble upon this when searching for the problem. All the “look it up” response does is make sure the search results are a bunch of content saying “look it up”, which isn’t really that helpful.

Re: Named element IDs can be referenced as JavaScript globals

#15
post #10
post #7

This is one of those things that pops up every year or two years. Unfortunately, the person writing about the new discovered weird trick almost always fails to precede the article with a big, red, bold "Please don't ever do this".

and then someone always follows up with "Please don't ever do this", without explaining WHY you should never do this: https://wikipedia.org/wiki/Wikipedia:Chesterton's_fence

The article already explains that thoroughly.

Re: Named element IDs can be referenced as JavaScript globals

#16
post #5

Now I'm worried of using IDs and finding issues with globals in JavaScript. Seems to be a curious issue to be debugged.

If you read the article and the spec, you'll see that any explicitly created variables will always take precedence over automatic IDs, so any globals will always override these IDs.

And that could be the problem if you try to access an element by id but a variable has the same name. This renders this option pretty useless.

Re: Named element IDs can be referenced as JavaScript globals

#17

Earlier quoted context omitted.

It's has been explained enough times. It's just that looking things up for yourself seems to have gone out of fashion.

That doesn’t help people who stumble upon this when searching for the problem. All the “look it up” response does is make sure the search results are a bunch of content saying “look it up”, which isn’t really that helpful.

That's a classic.

Get my hopes up finding an old forum post asking my question, hoping to find answers. All the answers are "use Google/etc", which is how I got there.

Re: Named element IDs can be referenced as JavaScript globals

#18
post #10

Earlier quoted context omitted.

and then someone always follows up with "Please don't ever do this", without explaining WHY you should never do this: https://wikipedia.org/wiki/Wikipedia:Chesterton's_fence

It's has been explained enough times. It's just that looking things up for yourself seems to have gone out of fashion.

lol, I just searched "problem with referencing named element ids as javascript globals": the first result is the linked article and the second result is, you guessed it, this thread with your comment on top.

Re: Named element IDs can be referenced as JavaScript globals

#19
Isn't the opposite of

>So, if a DOM element has an id that is already defined as a global, it won’t override the existing one.

So, if a global has name of the id of a DOM element, it won’t override the existing one?

Wouldn't it be clearer to say globals always before DOM ids?

Post reply on HN