Hacker News new | ask | show | jobs
by m90 2945 days ago
Sorry for being terribly pedantic, but the "vanilla" version just does not do what the jQuery code does:

- `this` might not be document

- find might return multiple elements, not just one

- if nothing matches, jQuery does not throw unlike the vanilla version

I do understand it's just an example, but if you'd consider all the implicit cases the jQuery example covers, you'd end up with a lot more than a single line. I see your point, but using off examples like this don't really sell it too well.

2 comments

assuming "this" is an element... `this.querySelector` if you're intending to match multiples...

    Array.from(document.querySelectorAll(...)).forEach(x => {...})
It's not *that hard... though forEach should be on your dom collection returned, if Array.from exists, but I started shimming Array.from long before either were standard.
These seem to be unintended side effects. From the example it seems the vanilla versions behaviour is what the author wants.