@tannerhodges/snap-slider
Version:
Simple JavaScript plugin to manage sliders using CSS Scroll Snap.
20 lines (17 loc) • 470 B
JavaScript
/**
* Get an attribute for the closest element with it.
* @param {Element} el
* @param {String} attr
* @return {String}
*/
export default function getClosestAttribute(el, attr) {
// Ignore missing elements
if (!el) {
return '';
}
// Find the closest element with a mattring attribute
el = el.closest(`[${attr}]`);
// If we found a match, return the attribute, otherwise
// return an empty string.
return el ? el.getAttribute(attr) : '';
}