Javascript is only returning HTML DOM attribute values when they are assigned by the javascript function, not when they are assigned by CSS. So, if I set the background colour of a div using CSS, then try to call that attribute value, JS doesn't return it, BUT, if I then assign a new value using JS, it can correctly read the attribute value.
What am I doing wrong? Or can javascript not pull HTML DOM attribute values assigned by CSS?
<html>
<head>
<style type="text/css">
.titlebar {background-color: #00ff00}
</style>
<script type="text/javascript">
function clicky()
{
bg = document.getElementById("title");
alert("Returning CSS-assigned value");
alert(bg.style.backgroundColor);
alert("Assigning new value");
bg.style.backgroundColor = '#ff0000';
alert("Returning new value");
alert(bg.style.backgroundColor);
}
</script>
</head>
<body>
<div id="title" class="titlebar" onclick="clicky()">
<h1>Blah Blah</h1>
</div>
</body>
</html>

New Topic/Question
Reply


MultiQuote




|