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
Named element IDs can be referenced as JavaScript globals
11–20 of 113 posts
Re: Named element IDs can be referenced as JavaScript globals
#12 document.getElementById
like I should have and everything worked fine. Like others in this thread, I recommend not relying on this behavior.Re: Named element IDs can be referenced as JavaScript globals
#13Now I'm worried of using IDs and finding issues with globals in JavaScript. Seems to be a curious issue to be debugged.
Re: Named element IDs can be referenced as JavaScript globals
#14Earlier 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.
Re: Named element IDs can be referenced as JavaScript globals
#15This 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
Re: Named element IDs can be referenced as JavaScript globals
#16Now 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
#17Earlier 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.
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
#18Earlier 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.
Re: Named element IDs can be referenced as JavaScript globals
#19>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?