News for hackers. Stuff that matters.
Re:GetElementById vs. QuerySelector by simlevesque on Thursday November 25, 2021 @10:48PM (#29347345)
Edit: seems like I'm wrong.

They are both completely different and almost no one mentions how they differ in these comparison blog articles.

querySelector return a static node while getElementById returns a live node. If the element returned by getElementById is deleted in the DOM, the variable becomes unavailable while for querySelector you get a snapshot of the node that lives on.

If you use both of them the same way or don't know the difference, you are gonna have a bad time.

https://developer.mozilla.org/en-US/docs/Web/API/Document_ob...

Re:GetElementById vs. QuerySelector by anderskaseorg on Thursday November 25, 2021 @11:14PM (#29347434)
You are misreading the documentation. There's no such distinction as live node vs. static node in that sense. There's only a distinction between a live node list and a static node list. This is a difference between getElementsByClassName("foo") and querySelectorAll(".foo"), but not between getElementById("foo") and querySelector("#foo").

The difference is whether membership changes in the collection are reflected immediately. Changes to the nodes themselves are reflected as usual either way, and node references do not spookily invalidate or repoint themselves.

Re:GetElementById vs. QuerySelector by kasajian on Friday November 26, 2021 @12:18AM (#29347662)
spookily?
Re:GetElementById vs. QuerySelector by JoelEinbinder on Friday November 26, 2021 @12:48AM (#29347792)
Its a reference to Einstein's spooky [action at a distance](https://en.wikipedia.org/wiki/Action_at_a_distance)
[dead]
Re:GetElementById vs. QuerySelector by rndgermandude on Friday November 26, 2021 @12:11PM (#29351994)
It's not part of javascript, it's part of the DOM API, and yes, that makes a difference. That's like calling C++ bad because you don't like boost, or calling python bad because you don't like django. (I am not saying tho that there isn't plenty to criticize in javascript itself).

The DOM API is not javascript specific. The API surface is defined in terms of WebIDL (Web Interface definition language). While it is most commonly found in browsers and therefore javascript, it is not specific to browsers or javascript. Browsers themselves ship various javascript environments that do not even include the DOM API at all (WebWorkers aka threads, ServiceWorkers, PAC all give you an environment without a DOM). nodejs doesn't come with a DOM (tho there is an implementation available in jsdom). There is nothing really preventing you from looking at the DOM spec and writing your own DOM API implementation for your preferred language. In fact Microsoft and Google used to do that for their in-browser support of vbscript and dart respectively, python ships with xml.dom (implementing an older version of the spec), java ships with org.w3c.dom (implementing an older version of the spec). There is a C# implementation in the AngleSharp library, somebody wrote a go implementation, and so on.

Re:GetElementById vs. QuerySelector by snovv_crash on Tuesday November 30, 2021 @02:15PM (#29396197)
So tell me, how would the invalidation work if the API were written in a language like Rust?

You've just listed a bunch of GCed languages and told me it's due to the API not the language. GC lets you be sloppy with resource initialization/cleanup though, and having GC doesn't magically fix when the external objects you interact with go in and out of scope.

So what I'm arguing above is that GCed languages are actually just as hard as manual memory managed languages the moment you need to interact with the real world... be it UI elements, file handles, network sockets, or whatever. Memory isn't the only resource that needs management, and languages that have idiomatic resource management are actually good at this kind of thing.

Re:GetElementById vs. QuerySelector by samastur on Friday November 26, 2021 @05:33AM (#29349007)
This is an API issue (DOM), not a language one.
Re:GetElementById vs. QuerySelector by snovv_crash on Tuesday November 30, 2021 @02:17PM (#29396225)
No, it's a language issue. The language doesn't let you specify that things cannot go out of scope 'spookily', so it is ambiguous and you have to trust things like documentation. Just like it's easy to make memory management issues in C, this is the kind of thing that's easy to get wrong in JS.
Re:GetElementById vs. QuerySelector by da_chicken on Friday November 26, 2021 @07:03AM (#29349481)
Deflecting to semantics or categorization isn't a defense.
Re:GetElementById vs. QuerySelector by samastur on Friday November 26, 2021 @09:26AM (#29350429)
My point it that it is a defined behaviour of the DOM API which is generally implemented in C++ and exposed to Javascript environment. It behave exactly the same way if you use bindings for any other language. It's not an ECMAScript defined feature.

You can also implement exactly this behaviour in any language so how is this then a Javascript language issue?

Re:GetElementById vs. QuerySelector by christophilus on Friday November 26, 2021 @08:07AM (#29349918)
Similarly, C is crappy because operating system APIs are crappy.
Re:GetElementById vs. QuerySelector by chrismorgan on Thursday November 25, 2021 @11:13PM (#29347430)
This is nonsense for querySelector and getElementById. They both query the current state of the DOM and return an Element or null. Variables can’t get deleted under you in JavaScript, so if you have a reference to a node that you remove from the DOM, it’s still the same node until you release all references and it gets garbage collected, or you put it back in the DOM.

What you’re talking about is only applicable or relevant for the methods that return collections. querySelectorAll returns a static NodeList, getElementsByName/getElementsByTagName/getElementsByClassName return live NodeLists or HTMLCollections.

Re:GetElementById vs. QuerySelector by mysecretaccount on Thursday November 25, 2021 @11:09PM (#29347412)
> querySelector return a static node while getElementById returns a live node. If the element returned by getElementById is deleted in the DOM, the variable becomes unavailable while for querySelector you get a snapshot of the node that lives on.

I'm mostly sure that this is not true: https://imgur.com/a/XaS3b0W

Re:GetElementById vs. QuerySelector by throwanem on Thursday November 25, 2021 @11:15PM (#29347443)
It makes sense that that would be the case. You still have a reference in scope to the element, so the GC leaves it alone. I could see the described behavior as a bug in maybe an older engine, but so far as I can recall I've never run into it in practice.
Re:GetElementById vs. QuerySelector by LeonB on Thursday November 25, 2021 @11:28PM (#29347489)
The responses to this are a fantastic example of Cunningham’s Law at work. Thank you!
Re:GetElementById vs. QuerySelector by rudasn on Friday November 26, 2021 @12:44AM (#29347780)
Yeah, I surprised to even see those two terms on an article on HN (who uses getElementById or querySelectors nowadays right?) but I was even more surprised by the number of comments it gathered (which are of course unrelated to the original article)
Re:GetElementById vs. QuerySelector by ligerzer0 on Friday November 26, 2021 @05:59AM (#29349123)
I use getElementById pretty often
Re:GetElementById vs. QuerySelector by nonima on Friday November 26, 2021 @10:12AM (#29350889)
What else would you use?
Re:GetElementById vs. QuerySelector by olavgg on Friday November 26, 2021 @02:44AM (#29348202)
I use them all the time.
Re:GetElementById vs. QuerySelector by bredren on Friday November 26, 2021 @03:37AM (#29348420)
I do as well. I often prototype behavior in Vanilla ES6+ JavaScript.
Re:GetElementById vs. QuerySelector by minitech on Thursday November 25, 2021 @11:10PM (#29347417)
That’s incorrect. Maybe you’re thinking of querySelectorAll, which returns a static list, compared to getElementsByTagName and getElementsByClassName, which return live ones?
Re:GetElementById vs. QuerySelector by tentacleuno on Friday November 26, 2021 @04:06AM (#29348566)
I wonder why we need all these different collections. Makes the DOM feel hacked together by lots of totally different people not communicating (?) There's probably a reason, though.
Re:GetElementById vs. QuerySelector by jfrunyon on Friday November 26, 2021 @07:34AM (#29349685)
That's because it was hacked together by lots of totally different people not communicating. Over a couple of decades, no less.
Re:GetElementById vs. QuerySelector by rndgermandude on Friday November 26, 2021 @12:21PM (#29352075)
Exactly. Once upon a time the powers that were figured a live node list would be cool, so they did that. Many moons later, the new powers that were decided that while a live node list looks cool, it contains too much magic pixie dust which may cause allergies in a lot of people (symptoms include frantically screaming at your screen about why this stuff doesn't work like you expect it to work). But they couldn't just change existing stuff or risk breaking the web. But they could avoid it for new stuff, and so they did that.
Re:GetElementById vs. QuerySelector by joshspankit on Saturday November 27, 2021 @02:15AM (#29358244)
For reference: https://xkcd.com/927/
Re:GetElementById vs. QuerySelector by sillysaurusx on Thursday November 25, 2021 @10:58PM (#29347374)
What an awful design. Why would it differ like that? No doubt some legacy baggage that was later turned into a justification.
Re:GetElementById vs. QuerySelector by jayflux on Friday November 26, 2021 @03:58AM (#29348522)
These APIs didn’t all come out the same time, so they weren’t designed to differ.

Instead they decided against doing live node lists in future but couldn’t change how the older methods work as that would break websites. In the world of DOM/JS you can’t really make breaking changes.

https://humanwhocodes.com/blog/2010/09/28/why-is-getelements... Has more context

Re:GetElementById vs. QuerySelector by Ozzie_osman on Thursday November 25, 2021 @11:07PM (#29347399)
I'd actually expect the opposite just from the names. I'd expect a selector to select a "live" node, and getElement to get a static one.
Re:GetElementById vs. QuerySelector by sodapopcan on Thursday November 25, 2021 @10:57PM (#29347369)
Whoa! As someone who likes to think they know a thing or two about vanilla JS, I did not know this. This is pretty dang important.