Page makes me want to keep using jQuery
julianfnunez opened this issue · 2 comments
I think this page has a bug. I believe the objective of this page is to demonstrate how jQuery is not needed and that it's just as easy to use native Javascript.
Unfortunately it has the opposite effect. It makes me appreciate how nice the jQuery API and syntax are and how they simplify code and add a lot of readability.
Take this for example: 1 line vs 8 lines including a magic number in it.
// jQuery (optional filter selector)
$el.prevAll($filter);
// Native (optional filter function)
function getPreviousSiblings(elem, filter) {
var sibs = [];
while (elem = elem.previousSibling) {
if (elem.nodeType === 3) continue; // ignore text nodes
if (!filter || filter(elem)) sibs.push(elem);
}
return sibs;
}
Please fix this page so it makes me want to not use jQuery anymore.
Another example, this one with multiple ways to do the same thing with different levels of compatibility, as opposed to a single line solution. Definitely makes me want to use jQuery.
// jQuery
$el.closest(selector);
// Native - Only latest, NO IE
el.closest(selector);
// Native - IE10+
function closest(el, selector) {
const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
while (el) {
if (matchesSelector.call(el, selector)) {
return el;
} else {
el = el.parentElement;
}
}
return null;
}
@julianfnunez jQuery helps a lot in the previous years when native API is not mature. But jQuery is not for the future since React/Vue can do a lot of DOM manipulation, and native APIs are evolving rapidly.