Skip to content

Instantly share code, notes, and snippets.

@jjt
Created March 4, 2011 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjt/853909 to your computer and use it in GitHub Desktop.
Save jjt/853909 to your computer and use it in GitHub Desktop.
//gets the first matching ancestor up the tree, or returns false
//cond can be 'tag', '.classname', '#id-name', Element
Element.implement({
grabParent: function(cond,include_self) {
var parents = this.getParents(),
true_cond = '',
ret = false,
el,
i;
include_self = include_self || false;
true_cond = (typeof(cond) == 'string') ? cond.substring(1) : cond;
if(include_self)
parents.unshift(this);
parents = parents.reverse();
for(i = parents.length; i > -1; i--) {
el = parents[i];
if(!el)
continue;
if(typeof(cond) == 'object' && cond.$family.name == 'element') {
if(el === true_cond)
return el;
continue;
}
if((cond.charAt(0) == '#' && $(el).get('id') == true_cond) || (cond.charAt(0) == '.' && $(el).hasClass(true_cond)) ||($(el).get('tag') == cond.toLowerCase()))
return el;
}
return false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment