diff --git a/README.md b/README.md index dc7f5a2..2abb440 100644 --- a/README.md +++ b/README.md @@ -1053,6 +1053,7 @@ Choices is compiled using [Babel](https://babeljs.io/) to enable support for [ES - Array.prototype.indexOf - Object.assign - Element.prototype.classList +- Element.prototype.closest - window.requestAnimationFrame - CustomEvent diff --git a/src/scripts/lib/utils.js b/src/scripts/lib/utils.js index cf3207b..24dc844 100644 --- a/src/scripts/lib/utils.js +++ b/src/scripts/lib/utils.js @@ -39,19 +39,11 @@ export const wrap = (element, wrapper = document.createElement('div')) => { return wrapper.appendChild(element); }; -export const findAncestorByAttrName = (el, attr) => { - let target = el; - - while (target) { - if (target.hasAttribute(attr)) { - return target; - } - - target = target.parentElement; - } - - return null; -}; +/** + * @param {HTMLElement} el + * @param {string} attr + */ +export const findAncestorByAttrName = (el, attr) => el.closest(`[${attr}]`); export const getAdjacentEl = (startEl, className, direction = 1) => { if (!startEl || !className) {