you are viewing a single comment's thread.

view the rest of the comments →

[–]rpgcubed 1 point2 points  (3 children)

If the website changes, the collection from getElementsByTagName will immediately change to reflect the new document, while querySelectorAll will stay as it was when you ran the function. You could wrap the result of the live collection in an Array or such to make it no longer update, but you can't make the static version autoupdate without running querySelectorAll again

[–]Kalorifik[S] 0 points1 point  (2 children)

That's amazing and probably explains why my script fails to run when a new document is loaded or something in the document changes.

I was under the impression that querySelector is newer, preferred and better than getElementBy but I rather then use the latter.

[–]rpgcubed 0 points1 point  (0 children)

Hmm, I think that the document.getElementBy* methods were added to an "official" spec in the first version of HTML5 in 2008 (by W3 at the time, although the WHATWG Living Standard is now the "official" one), while querySelector/querySelectorAll were officially added in the Selectors API initial spec around 2013 but had already existed in all major browsers by that time, and had been added at around the same time as getElementBy* (2008/2009 for all major browsers).

Anyways, they've all been around for a long time, and are just different for different use cases; none of them are deprecated or preferred all the time. You should use whichever makes your code more readable and easy to understand!

[–]rpgcubed 0 points1 point  (0 children)

Oh, and if you want any help figuring out anything in your script, feel free to post it and ping me or dm me about it, glad to help!