Skip to content

Instantly share code, notes, and snippets.

@thure
Last active January 5, 2017 15:05
Show Gist options
  • Save thure/8fb3e403c4408979732f2ee058867c1a to your computer and use it in GitHub Desktop.
Save thure/8fb3e403c4408979732f2ee058867c1a to your computer and use it in GitHub Desktop.
/**
* Climbs up the DOM up to but not including the limit element (or
* `body` if not specified) looking for and returning the first
* element that passes the predicate, or `null` if nothing does.
*
* @param {HTMLElement} start
* @param {function} predicate
* @param {HTMLElement} limit
* @returns {*}
*/
export default function(start, predicate, limit){
if(!start) return null;
var cursor = start,
lim = limit || document.body;
while(cursor !== lim) {
if(cursor === document.body) break;
if(predicate(cursor)){
return cursor
}else{
cursor = cursor.parentNode;
}
}
return null;
};
{
"name": "climb-es6",
"version": "0.0.1",
"main": "climb.js",
"author": "Will Shown <w@willshown.com>",
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment