5 ms·
Technically that breaks, you want: document.querySelectorAll('.my-class-selector').forEach(el => el.classList.add('active')) Personally I use the native A
by lhorie 6y ago
Technically that breaks, you want:
document.querySelectorAll('.my-class-selector').forEach(el => el.classList.add('active'))
Personally I use the native APIs because they aren't that hard to use (most of them, anyways) and I tend to be OCD about bundle sizes, but to each their own, I suppose.
- megous 6y agoYeah, oh so many years ago I realized that I use just a small subset of jQuery, and just rewrote those bits, to avoid having to inject whole of jQuery into all pages that I browse (I had some global https?://* userscripts, so I was basically injecting jQuery into every single page and frame). https://megous.com/dl/tmp/basic.js https://megous.com/dl/tmp/basic.js 1.3kB compressed. Much better. :) And I avoided having to rewrite my extensions with platform APIs. That was before FF killed all the joy in writing userscripts for me, because I was no longer able to install and update them just by copying them to mozilla folder and had to invoke a lot of clicking ceremony to just get my 20 scripts or so from git into firefox past the webextension limitations on storage/access to mozilla folder.
- akie 6y agoYou should totally open-source that, put it on NPM, and write an article or so about it.
- megous 6y agoEveryone will have different set of methods they use. Point of this is that if you need some method you can just add it in in 4-5 lines or remove one you don't need. So it's not the best fit for NPM. It's good fit for good old copy/paste into a project, if you're optimizing for size/memory use. :)
- maple3142 6y agoBTW, NodeList.prototype.forEach is not available on every browsers that supports querySelector/querySelectorAll, such as IE11: https://caniuse.com/mdn-api_nodelist_foreach https://caniuse.com/mdn-api_nodelist_foreach So you need to be careful about writing code like that if you want to avoid jQuery.