by DeathArrow · 4y ago · 0 comments
https://gist.github.com/pseudosavant/b86eedd9960ade958d49447...
One line more. But you have one dependency less, which is quite beneficial
A lot of times the jQuery syntax is easier to understand (IMO).
Remove element exmample:
jQuery API:
$('.element').remove()
DOM API: document.querySelector('.element').parentNode.removeChild(document.querySelector('.element'))
or if you want to introduce another variable just to remove the element: const child = document.querySelector('.element');
child.parentNode.removeChild(child);
Create element example:jQuery API:
const fooElement = $('<div class="foo" style="display: none">')
DOM API: const fooElement = document.createElement('div');
fooElement.classList.add('foo');
fooElement.setAttribute('style', 'display: none');
I know which ones I'd rather write and read.https://developer.mozilla.org/en-US/docs/Web/API/Element/rem...
Also for element creation:
Object.assign(document.createElement("div"), { class: "foo", style: "display:none" });
I mean, you normally abstract these things with a helper fn or two, without importing the whole jQuery anyway.
Add enough helper functions are you're at jQuery anyway. Looking at the jQuery source code, it manages a lot of edge cases that I don't really have time to figure out on my own.
The DOM API isn't really designed that well for human consumption.
https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeTy...
var xs = document.querySelectorAll('.x');
for (var i = 0; i < xs.length; i++) {
xs[i].addEventListener('click', function() {
this.style.display = 'none';
});
}
jQuery: $('.x').on('click', function() { $(this).hide() });
ಠ_ಠ document.body.addEventListener("click", ev => ev.target.classList.contains('x') && ev.target.hidden = true);JS is design by a committee full of people afraid to come out as "not-as-kind" and completely detached from reality.
True, doing some of the things which were half an hour on stack overflow or 1 line of jQuery became a couple of lines of vanilla JS but the API is still pretty awful.
Think about XHR > fetch vs $.get or $.post
I agree, I'll try to use vanilla JS and google something when I need to ship some code - but for hacking around I'll just bring in jQuery and write 1-liners at blazing speed.
These threads are completely ridiculous.