tags:

views:

820

answers:

5

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);
};
+3  A: 

I believe the correct way to do this is to check the .length property.

In your case you'd want to loop through all of your elements and check for the first one that meets

.css('background').length != 0

Read up on this at jqueryfordesigners

Rabbott
@David's answer is better, as far as using as boolean rather than length, but had to +1 this to... first answer which will work and needs some rep points.
sberry2A
+11  A: 

If it's not set, fetching it will yield an empty string. Hence

var bg = ('element').parents().filter(function() {
    return $(this).css('background').length > 0;
}).eq(0)

EDIT

Some research shows that css('background') will always yield an empty string, or undefined, 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 (transparent in IE, rgba(0,0,0,0) in firefox/chrome, for instance, both being accurate ways of specifying transparent).

jQuery.fn.getBg = function() {
    return $(this).parents().filter(function() {
        // only checking for IE and Firefox/Chrome. add values as cross-browser compatibility is required
        var color = $(this).css('background-color');
        return color != 'transparent' && color != 'rgba(0, 0, 0, 0)';
    }).eq(0).css('backround-color');
};
David Hedlund
brilliant solution +1
Gaby
what exactly is 'x' doing in function(x)? is that a placeholder or is it actually being used for something?
adamyonk
Try this: if ('') alert("The empty string is true!"); -- also you can convert a "truthy" value to boolean with !!.
Pointy
also, should '.length > 0' be '.length() > 0' ?
adamyonk
@adamyonk: you're right about `x`. slipped my mind there. the parameter being passed to the `filter` callback is actually a zero-based index, so that wouldn't have worked at all. as for length, no, it's a property, not a method. `length()` doesn't work on strings, nor does it on arrays, in javacript. it'd be needed in java, tho.
David Hedlund
ok, that makes sense. but now when i try to actually set that background-color value, it's not working, because 'bg' is [object Object] . What do I have to do to be able to set what's returned as a background? Here's my code:jQuery.fn.getBg = function(){var newBackground = this.parents().filter(function() {return $(this).css('background-color').length > 0;}).eq(0);this.css('background-color',newBackground);};
adamyonk
ah, yes, your question specified finding the first parent that has a background set, not finding what said background was ;) `bg` is a reference to the element that has the background of interest, so you'll have to modify the last part to `eq(0).css('background');`
David Hedlund
does this only work for the first parent? or does it supposed to go to the first parent WITH a background? i'm getting null if the FIRST parent doesn't have a background.
adamyonk
actually.. found out that if the FIRST parent ISN'T set, it returns a value of rgba(0,0,0,0)
adamyonk
you're right, turns out `background` is never the way to go, and `background-color` will give a different value for "transparent" depending on browser. check my updated answer.
David Hedlund
Thanks so much for working through this with me David!
adamyonk
+1  A: 
$('selector').parents().filter(function() { return !!$(this).css('background'); });
Pointy
at last a good use of the !! notation +1
Gaby
An empty string is false in Javascript, you know.
Pointy
@gaby oh I see what you meant Gaby; never mind! And thanks for the vote!
Pointy
A: 

I'd try something like this:

var background = 0;
$('element').parents().each(function(e) {
  if (!background && e.css('background')) background = e;
});

Not sure if this exact if-clause works, but the each() should do the trick.

hurikhan77
A: 

Hi Adam,

You could do something like:

$('#test').parents().has(css('background')).filter(':first');

Hope it helps : )

SDReyes
css is not a function out of the jQuery context.
Derrick
Agree with derrick, on top of that the .has() function takes an element selector as a its param.
JoseMarmolejos