Get the last element of an anonymous HTMLCollection

Advertisements My vanilla Javascript code contains many constructs like this: var foo = document.getElementsByClassName("foo")[0]; This works well whenever I want the first instance of foo (or when there is only one instance of foo). But, what if I want the last instance of foo? I can of course do: var fooCollection = document.getElementsByClassName("foo"); var foo… Read More Get the last element of an anonymous HTMLCollection

Javascript filter over HTMLCollection

Advertisements I want to get particular query params from all the js files which have been loaded by the browser. My query params reside in the js file named script.js(ex: In the browser console, I tried like this: const scripts = document.getElementsByTagName("SCRIPT") scripts.filter((script) => { return script.src.includes("script.js?code=")}) But this returns the error Uncaught TypeError:… Read More Javascript filter over HTMLCollection