/    Sign up×
Community /Pin to ProfileBookmark

document.getElementById().width returning unassigned.why?

Here is my js function:

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

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@mrhooJul 22.2006 — It looks like you have a div named 'news' and you are looking for an attribute called 'width'. Although images have width attributes, most elements do not. Try the offsetWidth attribute (parseInt it to get a number)
Copy linkTweet thisAlerts:
@ck_net_2004Jul 22.2006 — The div "news" width atribute is in css styling so it should be:

newsWidth = document.getElementById("news").style.width;
Copy linkTweet thisAlerts:
@sanchopansaauthorJul 22.2006 — problem solved. I used the offsetWidth and it worked out perfectly fine.

Thank you for your help.

Greetings,

V
×

Success!

Help @sanchopansa spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...