Skip to content

Commit

Permalink
add find parentByClass helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Mrowetz committed May 1, 2017
1 parent 2567cc5 commit 76488e2
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/ts/helpers/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,18 @@ export function forEachNodeList<T extends Node>(list: NodeListOf<T>, fn: {(el: T
fn(list.item(i), i);
}
}

/**
* Helper to recousivly find parent with `className`
* @param base `Element` to start from
* @param className class that the parent should have
*/
export function findParentByClassName(base: Element, className: string) {
if (base.parentElement === undefined) {
return undefined;
}
if (base.parentElement.classList.contains(className)) {
return base.parentElement;
}
return findParentByClassName(base.parentElement, className);
};

0 comments on commit 76488e2

Please sign in to comment.