Skip to content

Instantly share code, notes, and snippets.

@krutoo
Created January 11, 2020 07:31
Show Gist options
  • Save krutoo/d64fbd0e5f2e0f1b4d19a9c2e27f1cff to your computer and use it in GitHub Desktop.
Save krutoo/d64fbd0e5f2e0f1b4d19a9c2e27f1cff to your computer and use it in GitHub Desktop.
DOM shortcut utils
export const $ = selectors => document.querySelector(selectors);
export const $$ = selectors => document.querySelectorAll(selectors);
// on(div, 'click touch', () => {});
export const on = (target, eventNames, handler, options) => {
const eventNamesList = eventNames.split(' ');
eventNamesList.forEach(eventName => {
target.addEventListener(eventName, handler, options);
});
return () => {
eventNamesList.forEach(eventName => {
target.removeEventListener(eventName, handler, options);
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment