Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSS font-size not being carried thru to JScript code

Status
Not open for further replies.

decomplexity

IS-IT--Management
Dec 4, 2007
8
0
0
GB
Having looked at an utterly trivial problem for a couple hours with no resolution, I feel a complete fool and would be grateful if someone would point out what is wrong - I cannot see the wood for trees. (I have boiled the more complex real problem down to its core code).

In summary:
- ID selector 'foo' is assigned a font-size in inline CSS.
- 'foo' is defined in the body as a DIV
- object element 'bar' is obtained by a getElementById (of 'foo') and it is being defined (i.e. is not null or undefined) - the first alert says so!
- if the commented-out line is uncommented, the second alert of 'bar.style.fontSize' correctly displays '4em'
- but if it remains commented out, nothing is diplayed. Why pls?

<html>
<head>

<style type="text/css">
#foo {font-size:1em;}
</style>

<script language="JavaScript" type="text/javascript">
function func(){
var bar = document.getElementById("foo");
alert(bar); // says 'bar' is an HTML Div element
//bar.style.fontSize = "4em" //if uncommented, the next alert says '4em'
alert(bar.style.fontSize);} // displays nothing!
</script>

</head>
<body onload="func()">

<div id="foo">
</div>

</body>
</html>
 
Hi

JavaScript can only access directly the CSS values set by itself.

You could use [tt]getComputedStyle()[/tt] to get the CSS values set elsewhere. ( See for example thread216-1670874 for more. ) But note that the browsers will mostly return the values converted to the internal representation they use.

Feherke.
[link feherke.github.com/][/url]
 
Of course...!! (Bang head on table several times)
Many thanks, Feherke
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top