Get an elements style value as it is in the .css file

D

Dave

Hi

How do I, via javascript, get an elements style value as it is in
the .css file, and not the computed value.
For #span, in the code below, I would like to get the value 167% and
not 22px.

So are there an direct way to get that value just as you would when
using, window.getComputedStyle(element, pseudoElt) or element.style.

Thanks / Dave

test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="test.css" />
</head>
<body>
<p id="p">Lalala lalala <span id="span">hmm</span> lalala lalala</
p>
</body>
</html>

test.css
body{font-size:13px;}
#p{font-size:100%;}
#span{font-size:167%;color:green;}
 
M

Martin Honnen

Dave said:
How do I, via javascript, get an elements style value as it is in
the .css file, and not the computed value.
For #span, in the code below, I would like to get the value 167% and
not 22px.

So are there an direct way to get that value just as you would when
using, window.getComputedStyle(element, pseudoElt) or element.style.

Not really, what you can do is read out document.styleSheets and
document.styleSheets.rules/cssRules (rules for IE/Win, cssRules for
other browsers). But it is then the task of your code to read e.g the
selectorText of a rule and relate that to an element, there is no
function in the browser API to take an element node and return
stylesheet rules that apply. At least not when you script documents
loaded in a browser window. Mozilla extensions might have other APIs
available as Firebug displays stylesheet rules that apply.
 
D

Dave

Have got an solution using the document.styleSheets, but the problem
is that there have to be an existing rule. Which make it impossible to
get an inherited value for example.

But if there are no API function I guess I have to rethink the whole
setup. :(

Thanks for your reply. =)
 
D

David Mark

Dave said:
How do I, via javascript, get an elements style value as it is in
the .css file, and not the computed value.
For #span, in the code below, I would like to get the value 167% and
not 22px.
So are there an direct way to get that value just as you would when
using, window.getComputedStyle(element, pseudoElt) or element.style.

Not really, what you can do is read out document.styleSheets and
document.styleSheets.rules/cssRules (rules for IE/Win, cssRules for
other browsers). But it is then the task of your code to read e.g the
selectorText of a rule and relate that to an element, there is no
function in the browser API to take an element node and return
stylesheet rules that apply. At least not when you script documents
loaded in a browser window. Mozilla extensions might have other APIs
available as Firebug displays stylesheet rules that apply.


IE has currentStyle, which seems to be what the OP is after (not
exactly as worded of course.) Opera has it as well, IIRC. Of course,
that coverage is pretty useless on the Web.

Interestingly enough, IE has no way to get the computed style at all.
Most libraries just pass the "current" (also referred to as "cascaded"
in some docs) style off as computed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top