Skip to content

Instantly share code, notes, and snippets.

@lifo101
Created July 24, 2012 11:58
Show Gist options
  • Save lifo101/3169552 to your computer and use it in GitHub Desktop.
Save lifo101/3169552 to your computer and use it in GitHub Desktop.
jquery plugin to find the closest CSS style of a DOM element's parents.
// $().closestStyle()
// find the closest CSS style of a parent for a DOM element and apply it
// to the selected element. I use this mainly before calling
// .effect('highlight') so the backgroundColor will mesh properly.
// @example:
// $('.row').closestStyle('backgroundColor').effect('highlight');
(function($){
$.fn.closestStyle = function(attr, val){
var me = $(this);
me.parents().each(function(i){
var c = $(this).css(attr);
if (c != 'transparent') {
me.css(attr, c);
return false; // stop
}
return true; // strict compliance
});
return this;
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment