Forum Moderators: open
Also, it is not as straight forward as simply reading the style property (as this only reflects the value of inline styles). AND the property name you need to read differs from IE (fontSize) and Firefox, Opera (font-size).
Peter-Paul Koch (www.quirksmode.org) perfectly describes how to generically read styles from elements. For a complete explanation, see:
[quirksmode.org...]
I've modified Paul's function to specifically return the font size of the passed element (id), which works in IE, Firefox and Opera - but check what IE returns in your case!
function getFontSize(el) {
var x = document.getElementById(el);
if (x.currentStyle) {
// IE
var y = x.currentStyle['fontSize'];
} else if (window.getComputedStyle) {
// FF, Opera
var y = document.defaultView.getComputedStyle(x,null).getPropertyValue('font-size');
}
return y;
}alert('returned font size = ' + getFontSize('my_id'));
Hope that helps.
(ASIDE: In posting code fragments... is there any way to preserve (exactly) the white-space/indentation? I am using sqbr-pre and sqbr-code tags but a lot of the white space is being stripped)
I wrapped your code in <script type="text/javascript"></script> but it doesn't return anything to the page. I've never used javascript before so I have no idea what I'm doing here. How can I get the value returned to the page?
Also, is there a way to determine the actual pixel height of a div? That would be the pixel height of the div after it has been stretched vertically with rows of text.
- Grant
I wrapped your code in <script type="text/javascript"></script> but it doesn't return anything to the page.
Yes, that should do the trick, but you need to replace my_id (in the last line that starts "alert...") with the value of the id tag for the element you are trying to examine ie.:
<div id="mydiv">Blah blah...</div>
or <body id="mybody">
'mydiv' or 'mybody' in this case.
With the code above you should get a message box popup informing you of the value it finds. What do you ultimately want to do with this value? Display it in the body of the page somewhere? Or use it in further calculations?