5 ms·
document.querySelector('html') And for React: https://javascript.info/custom-elements https://javascript.info/custom-elements
by LNSY 4y ago
document.querySelector('html')
And for React: https://javascript.info/custom-elements https://javascript.info/custom-elements
- mejutoco 4y agoOther functions are much more difficult to replace $('.sth').closest('ul') To me jQuery is like Regex or Linq. I think it had a really good api.
- frosted-flakes 4y agoYou can also do this with DOM APIs, no? https://developer.mozilla.org/en-US/docs/Web/API/Element/closest https://developer.mozilla.org/en-US/docs/Web/API/Element/clo...
- augusto-moura 4y agoIs not the same thing as it is a Element method, not NodeList one, so you will would need to map it instead. Because NodeList doesn't have the .map method you would also need to transform it to a Array first. So the actually drop-in replacement of `$(foo).closest(bar)` would be something like: Array.from(document.querySelector(foo)).map(el => el.closest(bar))
- mejutoco 4y agoThis did not exist the last time I looked. TIL. Thank you!
- CraigJPerry 4y agoIs it harder or just a bit longer-winded? E.g. i'm thinking this is the same behaviour document.getElementsByClassName('sth')[0]?.closest('ul');
- mejutoco 4y agoThanks, this is now standard it seems. TIL
- zimpenfish 4y agoThat's not exactly the same, I don't think, because jQuery will give you all the `ul` that are closest to `.sth`, not just the first. You'd have to write a loop over the list returned from `document.getElementsByClassName('sth')` to get the same behaviour (because it doesn't look like the standardised `closest` works on lists.)
- CraigJPerry 4y agoIf i'm following right, i think this expression would match? Array.from(document.getElementsByClassName("sth")).map(e => e.closest("ul")) The jQuery is definitely nicer to read than this though
- zimpenfish 4y agoYeah, that seems to do the same.
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- SemanticStrengh 4y agoIt's not because you know how to do a hello world that you know how to program.
- DeathArrow 4y agoAnd you would replace $.getJSON by how many lines?
- nobleach 4y agoare we doing CORS? Adding headers? If it is just a one-off fetch, I'd probably long-hand it. If I intended to be fetching from many endpoints, I'd probably write a tiny little abstraction. I'm not exactly keen on loading a 30kb lib (gzipped) so that I can write (maybe) fewer lines for data fetching. Especially considering jQuery's async requests don't conform to the fetch standard. (They reject on 404s)