0

Suppose I have a div
<div id="div_a" style="background-image:url('xxx')"></div>

Then I can get the url info by using

document.getElementById("div_a").style.backgroundImage  

to get its url info.

However if the style is defined in CSS the code above would return blank

Anything wrong with it?

2
  • What do you mean by "in CSS" - in a separate CSS class? Commented Jun 1, 2012 at 23:28
  • oh sry for the ambiguity, I mean the style sheet linked in <head> Commented Jun 1, 2012 at 23:47

1 Answer 1

3

What you are looking for is the computed styles getComputedStyle

window.getComputedStyle(document.getElementById("div_a"), null).getPropertyValue('background-image')
Sign up to request clarification or add additional context in comments.

2 Comments

Which browsers does this work in, though, exactly? Getting the computed style is a traditionally very tricky business.
@Pekka works fine in IE9/FF12/Chrome19 not yet tested in other versions though. Not sure if it works in prior IE versions

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.