Is there a way to check an elements parents and find the first one that has a CSS background set and then return that background value?
Something like:
var background = $('element').parents().has(css('background'));
UPDATE:
This is the code I’m now using:
jQuery.fn.getBg = function(){
var newBackground = this.parents().filter(function() {
return $(this).css('background-color').length > 0;
}).eq(0).css('background-color');
$(this).css('background-color',newBackground); console.log("new background is: "+newBackground);
};
If it’s not set, fetching it will yield an empty string. Hence
EDIT
Some research shows that
css('background')will always yield an empty string, orundefined, depending on browser.css('background-color')will correctly return the color of the element at hand; but also different values for each browser, so it is troublesome to test (transparentin IE,rgba(0,0,0,0)in firefox/chrome, for instance, both being accurate ways of specifying transparent).