Last active
March 4, 2021 14:56
-
-
Save mythz/db1996b4b87942fab84997dff73a702d to your computer and use it in GitHub Desktop.
3 missing JS functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => { | |
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) { } | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment