News for hackers. Stuff that matters.
Re:GetElementById vs. QuerySelector by asddubs on Friday November 26, 2021 @06:32AM (#29349262)
I would seriously not recommend doing this, because it's terrible at communicating intent. Yes, it works, but someone else (if you're lucky) or you will be staring at a variable that seems to have come out of nowhere trying to figure out what's going on sooner or later. Especially if the HTML id inadvertently got changed without changing the variable in kind
Re:GetElementById vs. QuerySelector by chrismorgan on Friday November 26, 2021 @08:46AM (#29350137)
The spec appropriately points out that this is a fragile technique.

Also to avoid any doubt: if something’s called a golfing trick, you almost certainly shouldn’t use it on normal code.

I confess I use this technique on every page on my website, in my carefully-golfed light/dark mode switcher. (https://chrismorgan.info/blog/dark-theme-implementation/ has a slightly-expanded version of it, and uses a more sensible document.querySelector instead.)

Re:GetElementById vs. QuerySelector by orangepanda on Friday November 26, 2021 @07:23AM (#29349615)
It's great for quickly fiddling around in the dev console.

Similarly - $0 gives access to currently selected element

Re:GetElementById vs. QuerySelector by asddubs on Friday November 26, 2021 @07:27AM (#29349636)
sure, for throwaway code anything goes of course