When I write example-code for articles, I tend to omit selectors, because I want to focus on what the code does, rather than how to select a Node in the DOM. I assume most of Dev.to's readers knows how to do that anyway, and might want to use their own selectors (class-based, attribute-based, something else?) instead of just copy/pasting the code.
But I could be wrong! So thank you for your feedback — I've added this to the article:
It's best practice to use document.getElementById over the global variable because defining a local variable colorScheme would take precedence. It's also annoying to debug these global variables because they are not explicitly defined. When searching for something like colorScheme = you won't find anything, and finding something like colorScheme.addEventListener tells you nothing about the type of variable. However, finding document.getElementById tells you it's a DOM element.
How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
11 plus years* active enterprise development experience and a Fine art degree 🎨
Very detailed post! Great share! @madsstoumann Framing these screenshots would've been chef's kiss. I've built a simple OSS tool to help with screenshots with ease. Give it a shot and let me know what you think.
Michael is a curious and intellectually quiet computer science student from the USA. He likes games and sandwiches. He needs to meet his goal of becoming a full-stack dev, game dev, and software dev.
Location
Usa, Nowhere (Private)
Education
Barbara Goleman High School
Work
Game developer, web developer, and software developer
What is the general consensus in the community about referencing elements like this? As opposed to:
Isn't it considered unsafe and error-prone? I don't really remember, it's rarely seen in the wild, perhaps for a reason.
When I write example-code for articles, I tend to omit selectors, because I want to focus on what the code does, rather than how to select a Node in the DOM.
I assume most of Dev.to's readers knows how to do that anyway, and might want to use their own selectors (class-based, attribute-based, something else?) instead of just copy/pasting the code.
But I could be wrong! So thank you for your feedback — I've added this to the article:
It's best practice to use
document.getElementById
over the global variable because defining a local variablecolorScheme
would take precedence. It's also annoying to debug these global variables because they are not explicitly defined. When searching for something likecolorScheme =
you won't find anything, and finding something likecolorScheme.addEventListener
tells you nothing about the type of variable. However, findingdocument.getElementById
tells you it's a DOM element.I vote that implicit code is dangerous code and explicit code is safe code and there is no grey area 🫣
(see updated feedback above)
Very detailed post! Great share! @madsstoumann Framing these screenshots would've been chef's kiss. I've built a simple OSS tool to help with screenshots with ease. Give it a shot and let me know what you think.
github.com/ekqt/screenshot
Cool!
Very cool.
I've used this scheme of system themed colours when programming Windows desktop apps.
I didn't know it existed in CSS though - thanks!
👍🏻
I love it.
👍🏻
Love this ❤️, Thanks for sharing.
👍🏻
This is something I'm for, Thanks for sharing! 🚀
👍🏻
Great tips, this was exactly what I've been looking for! 👏
👍🏻
Amazing code snippet.. thanks for telling.
You just saved a lot of time ⏲️ 😌
👍🏻
If statement missing here:
A notice that the article refers exclusively to the latest version of Chrome should be included at the very beginning.
??? System colors work in all browsers
Just test your code in all browsers and you'll get bad news. The browser war is still here.