Named element IDs can be referenced as JavaScript globals
81–90 of 113 posts
Re: Named element IDs can be referenced as JavaScript globals
#82Re: Named element IDs can be referenced as JavaScript globals
#83Earlier quoted context omitted.
Is there a reason to not use querySelector, since it’s a lot more flexible? One reason jQuery became so popular is because the DOM was painful to use. Things like querySelector fix that.
BTW, in my experience getElementById() is still fastest.
Re: Named element IDs can be referenced as JavaScript globals
#84This has been a thing since the 90s. I really wish we'd done away with it for any document that specifies itself as HTML5. It's great for hacking a tiny script together, however.
IDs were the only way to get a reference to an element early on if I'm remembering correctly. Or maybe the DOM API just wasn't well known. All the examples and docs just used IDs, that I can remember for sure.
Re: Named element IDs can be referenced as JavaScript globals
#85Re: Named element IDs can be referenced as JavaScript globals
#86I don't think this article is complete. It mentions no pollution, which is true of window and most HTML elements, but not always. Check this out, you can set an img name to getElementById and now document.getElementById is the image element! Here's a minimal example ( https://jsfiddle.net/wc5dn9x2/ ): // The img object console.log(document.getElementById); // TypeError: document.getElementById is not a function :D co…
Re: Named element IDs can be referenced as JavaScript globals
#87Earlier quoted context omitted.
Is there a reason to not use querySelector, since it’s a lot more flexible? One reason jQuery became so popular is because the DOM was painful to use. Things like querySelector fix that.
> Is there a reason to not use querySelector getElement is slightly faster, but not by enough to care IIRC so I use querySelector for consistency and it's flexibility. > One reason jQuery became so popular is because the DOM was painful I would say that is the key reason, with everything else being collateral benefits. Assuming you combine element selection, dealing with legacy incompatibilities, and function chainin…
Re: Named element IDs can be referenced as JavaScript globals
#88Earlier quoted context omitted.
You shouldn't be using IDs anyways. They are just bad for a lot of reasons. You can only have one on a page and it reduces your reusability. Use classes instead.
Use ids when JS needs to reference unique elements. Use classes for styling and accessing groups.
Re: Named element IDs can be referenced as JavaScript globals
#89This 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".