Skip to content

Instantly share code, notes, and snippets.

@VitorLuizC
Last active February 21, 2019 19:24
Show Gist options
  • Save VitorLuizC/958361b6c218360e00f712499eb8160d to your computer and use it in GitHub Desktop.
Save VitorLuizC/958361b6c218360e00f712499eb8160d to your computer and use it in GitHub Desktop.
/**
* Select an element that matches CSS selector.
* @param {string} selector - A CSS selector to match an element.
* @param {Document | HTMLElement} [from]
* @returns {HTMLElement}
*/
export const select = (selector, from = document) => {
return from.querySelector(selector);
};
/**
* Select elements that matches CSS selector.
* @param {string} selector - A CSS selector to match elements.
* @param {Document | HTMLElement} [from]
* @returns {HTMLElement[]}
*/
export const selectAll = (selector, from = document) => {
return Array.from(from.querySelectorAll(selector));
};
/**
* Exclude element from DOM.
* @param {HTMLElement} element
*/
export const exclude = (element) => {
if (element && element.parentNode)
element.parentNode.removeChild(element);
};
/**
* Get an array of ascendancy nodes.
* @param {HTMLElement} element
* @returns {HTMLElement[]}
*/
export const ascendancy = (element) => {
const parent = element.parentNode;
return !parent ? [] : [parent].concat(ascendancy(parent));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment