Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I add the parent element to mine

    const $ = (selector, parent = document) => parent.querySelector(selector);
    const $$ = (selector, parent = document) => parent.querySelectorAll(selector);


This is the way. In addition to `$` and `$$`, I usually have `$h` for creating elements:

    /**
     * @param {string} name
     * @param {HTMLElement.prototype} props
     * @param {Array<HTMLElement|string>} children
     * @return HTMLElement
     */
    function $h(name = 'div', props = {}, children = []) {
        const el = document.createElement(name);
        Object.assign(el, props);
        el.append(...children);
        return el;
    }




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: