2

Given a DIV of a page, I would like to get by Javascript what is the stylesheet inherit for the A elements.

So, for example

<html>
<head>
BODY { font-size:18px; }
A { color: red; }
#layer A { color: green; }
</head>

<div id="layer">
<a href="xxx">yyy</a>
</div>

</body>
</html>

I would like a JS function like getStyle("layer", "a") and it returns me "font-size:18px;color: green;" , that is all the styles applied to that element of that div, inherited from all the stylesheet.

Thank you !

FlamingMoe
  • 2,709
  • 5
  • 39
  • 64
  • excuse me, i don't know what you are talking about ... can't you reply to this ? i don't know what to do ... other questions I did had no problems ... seriously, what do i have to do ! :( – FlamingMoe Mar 07 '11 at 21:10
  • 3
    Please go to your previous asked questions and click the answer check next to the answer which helped you. – pimvdb Mar 07 '11 at 21:11
  • done, sorry ! ... and ty – FlamingMoe Mar 07 '11 at 21:13
  • Thanks. Anyway, this question might help you: http://stackoverflow.com/questions/1169967/inherited-css-values-via-javascript. – pimvdb Mar 07 '11 at 21:15
  • Yes, I was there, but it's not the same ... I need the inherited styles inside a given DIV of a given tag – FlamingMoe Mar 07 '11 at 21:18

1 Answers1

3

getComputedStyle is what you're looking for.

I made this on JSFiddle: http://jsfiddle.net/6MmAf/1/

<div id="layer">
<a href="xxx" id="thelink">yyy</a>
</div>

BODY { font-size:18px; }
A { color: red; }
#layer A { color: green; }

window.alert(
    getComputedStyle(
        document.getElementById('thelink')
    ).fontSize
);
// it alerts 18px, which is inherited from BODY in css
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • THANK YOU, but as I specified, I need the complete css applied to that element, therefore "font-size:18px;color: green;" :P Besides, I can not modified the original HTML – FlamingMoe Mar 07 '11 at 21:43
  • the updated MDN link: https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle – PIoneer_2 Mar 25 '23 at 14:02