Skip to content

Instantly share code, notes, and snippets.

@mythz
Last active March 4, 2021 14:56
Show Gist options
  • Select an option

  • Save mythz/db1996b4b87942fab84997dff73a702d to your computer and use it in GitHub Desktop.

Select an option

Save mythz/db1996b4b87942fab84997dff73a702d to your computer and use it in GitHub Desktop.

Revisions

  1. mythz revised this gist Mar 3, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions vanilla.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    let $ = sel => document.querySelector(sel),
    $$ = sel => document.querySelectorAll(sel);
    let $ = (sel, el) => typeof sel === "string" ? (el || document).querySelector(sel) : sel || null,
    $$ = (sel, el) => Array.prototype.slice.call((el || document).querySelectorAll(sel));

    function on(sel, handlers) {
    $$(sel).forEach(e => {
  2. mythz created this gist Jan 19, 2021.
    29 changes: 29 additions & 0 deletions vanilla.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    let $ = sel => document.querySelector(sel),
    $$ = sel => document.querySelectorAll(sel);

    function on(sel, handlers) {
    $$(sel).forEach(e => {
    Object.keys(handlers).forEach(function (evt) {
    let fn = handlers[evt];
    if (typeof evt === 'string' && typeof fn === 'function') {
    e.addEventListener(evt, fn.bind(e));
    }
    })
    });
    }

    /* Usage Examples: */

    $("#preview").innerHTML = "...";

    $$(`a[href="${hash}"]`).forEach(el => el.classList.add('active'));

    on('.hero .tab', {
    click: function(evt) { this.classList.toggle('active') }
    })
    on('form input', {
    keyup: function(evt) { }
    })
    on('.menu select', {
    change: function(evt) { }
    })