5 ms·
Update: fixed formatting A lot of times the jQuery syntax is easier to understand (IMO). Remove element exmample: jQuery API: $('.element').remove() DOM
by pseudosavant 4y ago
Update: fixed formatting
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.
- mikojan 4y agoOr you could just do document.querySelector('.element').remove()
- egeozcan 4y agoHTMLElement.prototype.remove exists everywhere except IE: https://developer.mozilla.org/en-US/docs/Web/API/Element/remove 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.
- cogman10 4y agoIE 11 hits it’s EOL next month. So, really probably not a whole lot of reason not to just pull the trigger and inform everyone that complains of this fact.
- wvenable 4y ago> 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/nodeType https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeTy...
- egeozcan 4y agoYou need to write a lot of helpers to come close to jQuery. Also edge cases aren't that big of a deal now that IE is near it's EOL.
- wvenable 4y agoI'm not talking about browser edge cases, I'm talking about API edge cases. The DOM API is not very human-friendly.
- cxr 4y agoI don't find the jQuery API to be particularly friendly. It tries to be cute and ends up being annoying. Its implementing code is also an abomination, so not particularly helpful. I'd rather read through Gecko internals than try to figure out what the hell a jQuery helper actually returns. The DOM is at least very, very well documented. jQuery has documentation, but it's not even close. It gives away what jQuery always has been, which is a library that is aimed mainly at assisting bag-of-tricks StackOverflow programmers than people who actually want to understand their programs and the object graph they work on.
- ratww 4y ago> Add enough helper functions are you're at jQuery anyway. I really hate these exaggerations. jQuery is, last I checked, over 200k unminified. Anyone writing enough helper functions to get to a fraction of that is already a pathological case.
- wvenable 4y agoBrowsing the source of jQuery and I don't find it bloated but I find a lot of knowledge in there that a naive user the DOM API would most likely know about. It's kind of like how you can comparing languages using simple example code isn't relevant because proper code with full error handling and edge case handling can be very different from a naive example.