views:

126

answers:

2

Why is the first alert box empty, but the second one says 100px? I'd expect the first box to say 300px..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Move</title>
    <style type="text/css">
div#b-ball {
  position:absolute;
  top:300px;
  left:300px;
}
</style>
<script type="text/javascript">
<!--
function moveBall() {
 alert(document.getElementById("b-ball").style.top);
 document.getElementById("b-ball").style.top="100px";
 alert(document.getElementById("b-ball").style.top);
}
//-->
</script>
</head>
<body onload="moveBall()">
   <div id="b-ball">
      Basketball image here.
   </div>     
</body>
</html>
A: 

Why would the second box say 300px? You've just explicitly set the style (which trumps the class) to 100px. The 'style' collection only refers to inline styles, not class definitions.

cletus
The second box says 100px. I wrote that I expected the first box to say 300px.... I say why it doesn't thanks to Guffa.
Peter Ajtai
+1  A: 

The style collection contains only the styles that are applied directly to the element, not the styles inherited from a style sheet.

Guffa
Thanks Guffa. Makes things a little more complicated.
Peter Ajtai
This will let you read the applied styles: http://my.safaribooksonline.com/9780596514082/Ireading_effective_style_sheet_property_values
Guffa