Skip to content

Instantly share code, notes, and snippets.

@caub
Last active September 30, 2018 21:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save caub/437e5d98ee9260df811628f7900c1c84 to your computer and use it in GitHub Desktop.
Save caub/437e5d98ee9260df811628f7900c1c84 to your computer and use it in GitHub Desktop.
v4.1
function $(container, selector) {
const els =
typeof selector === 'string'
? container.querySelectorAll(selector)
: typeof container === 'string' ? document.querySelectorAll(container) : [container];
const fns = {
removeClass(...cls) {
els.forEach(el => {
el.classList.remove(...cls);
});
return this;
},
addClass(...cls) {
els.forEach(el => {
el.classList.add(...cls);
});
return this;
},
toggleClass(cls, b) {
els.forEach(el => {
el.classList.toggle(cls, b);
});
return this;
},
on(type, cb, opts) {
els.forEach(el => {
el.addEventListener(type, cb, opts);
});
return this;
},
off(type, cb, opts) {
els.forEach(el => {
el.removeEventListener(type, cb, opts);
});
return this;
},
css(props) {
els.forEach(el => {
Object.assign(el.style, props);
});
return this;
},
};
return new Proxy(els, {
get(targ, k) {
return els[k] || fns[k];
},
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment