Hacker News new | past | comments | ask | show | jobs | submit login

Wow, I had no idea elements with an id attribute are directly accessible in JS.

Combine this with Pico CSS or one of those minimal stylesheets (Water.css) to get better typography and spacing and you‘re set.

I guess htmx is the extreme version of this no-framework philosophy… kinda.






Wait, this actually says to use the implicit ID mapping on the global object? That hasn't been good advice since about 1999!

edit: I checked and wow, it really does. Good grief, did an AI write this? Use getElementById or querySelector or querySelectorAll or any other remotely modern web API method instead.


Genuinely curious (as someone who didn't know about the implicit ID mapping until today): given that IDs are global, why is doing this any worse (or in any way different) to using getElementById? I.e. why is it bad advice?

This SO answer does a good job covering the reasons why, or all the reasons at least that I recall learning it was a bad idea. There may be more, but I'd probably have to go spec diving to find them.

Offhand these days I would expect asynchronous loading and rendering both to pose additional concerns. You may not be able even to guarantee whether or not the element you're trying to reference by ID exists at the time of reference. Oh, you can synchronize on DOMContentLoaded or something like that, but that's not going to do anything good for performance. And for that matter, you'd probably have to toggle a TypeScript compiler option I've never heard of, to get this sort of thing to even sort of fly - and there again, the API methods dating from this millennium will be mostly the ones where the type system is able to offer some guarantees.

https://stackoverflow.com/a/24606307


IE started this. Now IE is gone and we will remain stuck with it forever.

"Stuck?" I think it's marvelous.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: