5 ms·
Sure, but the runtime is not exactly designed to guide you to the best way to do this. Hence the prevalence of frameworks to paper over the runtime. I openly a
by cmonBro22 1y ago
Sure, but the runtime is not exactly designed to guide you to the best way to do this. Hence the prevalence of frameworks to paper over the runtime.
I openly admit that I'd rather learn a new framework than touch anything to do with figuring out how the browser is intended to behave in practice. What an abomination and insult to humanity.
Edit: holy shit y'all do not like the earnest approach to technology
- hdjrudni 1y agoHuh? The built-in APIs aren't perfect, but we're talking about something simple as ``` fetch('/my-content').then(async res => { if(res.ok) { document.getElementById('my-element').innerHtml = await res.text(); } }) ``` Something like that. Doesn't get much easier. Gone are the days of browser inconsistencies, at least of you stick to "Baseline Widely available" APIs which is now prominently displayed on MDN.
- rhet0rica 1y agoNo-framework web tinkerer here. If I had a nickel for every second of my life I've spent typing document.getElementById, I'd be able to afford new fingers. Should've been renamed to getId() and put in global scope two decades ago, if not three. At least querySelector() is a few characters shorter, but I always feel bad using such an alarmingly overdesigned tool for anything trivial.
- pests 1y agoYou don’t have to call getElementById or querySelector on document. You can narrow the search by starting at a specific node, just most people default into document as it’s already a global.
- someothherguyy 1y ago> You don’t have to call getElementById or querySelector on document. You do have to call `getElementById` on a document. There can be many documents in a window.
- pests 1y agoAh yes correct on getElementById, especially as every id must be unique.
- Jaygles 1y ago> especially as every id must be unique. Although a very consistent convention, there are no guardrails put in place to prevent something from setting the same id on two or more elements. getElementById will return the first element that it finds with the id, but you can't know for sure if it is the only one without additional checks
- insin 1y agoIf I'm just adding sprinkles of interactivity to a statically-rendered page for something that vanilla is good enough for (i.e. minimal-to-no display logic), I use the expando giving an element an `id` adds to `window`: <textarea id="userNotes"></textarea> <button type="button" id="copyButton">Copy</button> <script> copyButton.addEventListener('click', () => { navigator.clipboard.writeText(userNotes.value) copyButton.innerText = 'Copied' setTimeout(() => copyButton.innerText = 'Copy', 1000) }) </script>
- someothherguyy 1y agoWhy are you writing it so many times? Write an alias in 10 seconds? function getId(v) {return document.getElementById(v)} Dev tools allows $ $$, dunno, make a macro?
- cmonBro22 1y agoSorry?
- deleted 1y ago[deleted]
- rhet0rica 1y agoBut if Brendan Eich wanted us to have a shorter name, he would have given us a shorter name! We must not defy the system!
- BrendanEich 1y agoAs I said to Lex Fridman, I was influenced by AWK. Ragrets!
- deleted 1y ago[deleted]
- akoboldfrying 1y agoJavaScript has functions: const g = (x) => document.getElementById(x);
- kentbrew 1y agoThat's a code smell. If you're creating the element you ought to already know about it. If you're not creating the element you should inventory them all at once as the page (or component) loads.
- cmonBro22 1y ago...what is this simple compared to?
- deleted 1y ago[deleted]
- esseph 1y agoOh no, you're part of the problem :(
- andrewflnr 1y agoI guess openly admitting that you refuse to learn how your platform actually works is an "earnest approach", of a sort, but so is admitting that you routinely leave grocery carts in the parking lot and don't see a problem with it.
- giancarlostoro 1y agoTheres even an insane amount of documentation on the web about the web. MDN is very well maintained to boot. Im not as versed as some on front end web, but I recognize that I can slice through a lot of front end stuff by just referring to MDN.