3 ms·
Your example does not work. querySelectorAll returns array and you can't write `array.classList.add("...")`, because array does not have that property. You need
by vbezhenar 6y ago
Your example does not work. querySelectorAll returns array and you can't write `array.classList.add("...")`, because array does not have that property. You need to iterate over that array. And that's exactly why jQuery is so much more convenient over native API: you can just treat collections of elements like an element and jQuery will iterate for you if necessary.
Another jQuery advantage is pseudo-selectors which don't exist in CSS.
There are plenty of convenient one-liners in jQuery which will expand to plenty of lines in native API. That said, with modern API and JS features it's not that bad either.
- mkl 6y agoquerySelectorAll returns a NodeList, which is even more limited than an array (e.g. no map or filter).
- t_tsonev 6y agoJust a nitpick, but querySelectorAll returns a NodeList. This can bite at times, for example IE supports Array.forEach, but not NodeList.forEach
- dceddia 6y agoYeah this gets me every time. You can .forEach a NodeList, but can't .map. I usually end up wrapping it in an array and calling map on it like [...theNodeList].map(...)