-
-
Save argyleink/6811428290ac41cab8d4dab94b9edf97 to your computer and use it in GitHub Desktop.
add some sugar to bling dot js
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
const sugar = { | |
on: function(names, fn) { | |
names | |
.split(' ') | |
.forEach(name => | |
this.addEventListener(name, fn)) | |
}, | |
setAttributes: function(attrs) { | |
Object.entries(attrs) | |
.forEach(([key, val]) => | |
this.setAttribute(key, val)) | |
} | |
} | |
// shorthand querySelector: $('.foo') $('.child', context_node) | |
export const $ = (query, $context = document) => | |
Object.assign($context.querySelector(query), sugar) | |
// shorthand querySelectorAll: $$('.foo') $$('.child', context_node) | |
export const $$ = (query, $context = document) => | |
Object.assign( | |
[...$context.querySelectorAll(query)] | |
.map($el => Object.assign($el, sugar)) | |
, { | |
on: function(names, fn) { | |
this.forEach($el => $el.on(names, fn)) | |
}, | |
setAttributes: function(attrs) { | |
this.forEach($el => $el.setAttributes(attrs)) | |
} | |
}) |
the above, with some other extras, is now available from npm https://www.npmjs.com/package/blingblingjs
npm i blingblingjs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
some example test usage: