Skip to content

Instantly share code, notes, and snippets.

@impressiver
Last active December 31, 2015 04:09
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 impressiver/7932714 to your computer and use it in GitHub Desktop.
Save impressiver/7932714 to your computer and use it in GitHub Desktop.
Traverse the DOM hierarchy and return the first ancestor that has all/any css attributes that match the provided hash. Expects jQuery, but could be easily modified to work w/o. Main use for this is in Chrome dev tools to quickly find the parent of an element that's responsible for layout troubles.
//
// Traverse the DOM hierarchy and return the first ancestor that has all/any
// css attributes that match the provided attribute:regexp hash.
//
var $parent = (function declares(el, attrs, any) {
var $el = $(el), fn = (!!any ? 'some' : 'every'), match;
if(!attrs || !$el.length || $el.is(document)) return undefined;
match = Object.keys(attrs)[fn](function(key) {
var css = $el.css(key), matcher = attrs[key];
if(matcher === '*' && css !== undefined) return true;
(matcher instanceof RegExp) || (matcher = new RegExp(matcher, 'i'));
return matcher.test(css);
});
return match ? $el : declares($el.parent(), attrs, any);
})($('.deep-in-the-weeds'), {'overflow': /(hidden)|(auto)/i});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment