4 ms·
Is it harder or just a bit longer-winded? E.g. i'm thinking this is the same behaviour document.getElementsByClassName('sth')[0]?.closest('ul');
by CraigJPerry 4y ago
Is 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.