0

I have an iframe and I would like to set up the color white if the background is black and vice-versa. I tried to get the background color with:

$("#id").css('background')

and

$("#id").css('background-color')

and

$("#id").css('backgroundColor')

but they all return transparent.

Please help me.

4
  • 1
    could it be that #id's background is transparent? Commented May 30, 2012 at 11:42
  • if it's transparent then how do I get the parent background? Commented May 30, 2012 at 11:44
  • I test $("#id").css("background-color")) and return color in format rgb(255, 0, 0). Commented May 30, 2012 at 11:48
  • I didn't set background-color anywhere i just set background: #000000 (or other color). is it because of that? Commented May 30, 2012 at 11:51

2 Answers 2

2

You need to be looking at the background of the iframe's body, not the background of the <iframe> element itself.

var bgcolor = $(document.getElementById('iframeId').contentWindow.document.body).css('background-color');

(won't work if iframe content is from a domain different from yours)

Sign up to request clarification or add additional context in comments.

5 Comments

Uncaught ReferenceError: getElementById is not defined
Apparently not one of my best days. Fixed.
yeah ... the body has transparent too. But still my background is black
The background color might be from some other element within the iframe. It won't be trivial to determine heuristically which element gives the background color of the content.
1

I think you can walk the parents until you get a background-color that is not transparent:

var elm = $("#id")[0],
    bg;

do {

    bg = $(elm).css( "backgroundColor" );
    elm = elm.parentNode;
} while( elm && bg.toLowerCase() === "transparent" );


console.log( bg );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.