4 ms·
A number of these are using older, awkward APIs, when there's nicer native versions now. For example, "replace an element" is ele.parentNode.replaceChild(
by bakkoting 3y ago
A number of these are using older, awkward APIs, when there's nicer native versions now.
For example, "replace an element" is
ele.parentNode.replaceChild(newEle, ele);
but unless you're targeting Chrome < 54, FF < 49, or Safari < 10, you can just do
ele.replaceWith(newEle)
`replaceWith` also lets you replace a node with _multiple_ nodes, or with text.
Similarly, "loop over a nodelist" wants you to use array spread and then forEach, instead of just
for (let ele of nodelist) ...
- akira2501 3y agoGood to keep in mind that nodelist is a live object. So manipulating that list while looping over it can yield surprising results.
- Kailhus 3y agoOr “is an element within viewport”, we now have IntersectionObserver for that sort of stuff. I guess it could be combined with the rects but if I remember correctly, the logic would fail if the element was bigger than the viewport.