Forum Moderators: open
function setNewsPosition()
{
position=document.getElementById("image_1").width;//returns value
newsWidth= document.getElementById("news").width;//returns unassigned
alert(position);
var news= document.getElementById("news");
news.style.left= position + newsWidth + 'px';
}
Here is the css part that concerns the function:
div#news
{
float:left;
width:25%;
margin-left:-25%;
background:#0099cc;
}
I first thought it is because of the percentage value, but I've switched to px and it still returned unassigned. Does anyone have a suggestion why this might be and how it could be fixed.
Greetings,
Valentin
function getStyle(el,styleProp)
{
var x = document.getElementById(el);
if (x.currentStyle)
var y = x.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
return y;
} For your problem, you'd use the following code:
function setNewsPosition()
{
position=document.getElementById("image_1").width;
newsWidth=getStyle("news", "width");
var news= document.getElementById("news");
news.style.left= position + newsWidth + 'px';
} You could speed things up slightly by modifying the getStyle() function to accept an object instead of an id string and therefore only do one getElementById() but that's your call.
[edited by: Robin_reala at 11:37 am (utc) on July 22, 2006]