logoalt Hacker News

majewskytoday at 9:12 AM1 replyview on HN

Nearly every time I write something in JavaScript, the first line is const $ = (selector) => document.querySelector(selector). I do not have jQuery nostalgia as much as many others here, but that particular shorthand is very useful.

For extra flavor, const $$ = (selector) => document.querySelectorAll(selector) on top.


Replies

SahAssartoday at 1:48 PM

    const $$ = (selector) => Array.from(document.querySelectorAll(selector))
is even nicer since then you can do things like

    $$('.myclass').map(e => stuff)