/    Sign up×
Community /Pin to ProfileBookmark

style.width returning NaN?!

I have an element that I grab using getElementsbyName(‘navItem1’), the element has styling in it AND it has a fixed width of 106px, but, whenever I try to grab this width it returns a NaN and when I check the returned element using Firebug, it has absolutely no styling at all ????

The code is incredibly simple:

[CODE]
var activeElems = document.getElementsByName(elem);
alert(activeElems[0].style.width);
[/CODE]

It returns the actual element and I can set a width for it etc, but it refuses to give me the initial styling properties? Does anyone have any idea what the problem could be?

Thanks in advance

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsJun 12.2009 — unless the style is specified inline

you will need to obtain CCS class rule or inline defined width

[CODE]function zxcSVInt(obj,par){
if (obj.currentStyle) return parseInt(obj.currentStyle[par.replace(/-/g,'')]);
return parseInt(document.defaultView.getComputedStyle(obj,null).getPropertyValue(par.toLowerCase()));
}
[/CODE]


or

[CODE]function zxcSV(obj,par){
if (obj.currentStyle) return obj.currentStyle[par.replace(/-/g,'')];
return document.defaultView.getComputedStyle(obj,null).getPropertyValue(par.toLowerCase());
}
[/CODE]


wher par = 'width' and obj = activeElems[0]

[CODE]alert(zxcSV(activeElems[0],'width'));
[/CODE]
Copy linkTweet thisAlerts:
@temple3188authorJun 12.2009 — Thanks Vic! Works a treat!

Still can't figure out whats going on though, I thought I could access and manipulate the CSS values with element.style ...etc.

Could you explain the above code or even better post a link to a relevant subject?

Thank in advance!
×

Success!

Help @temple3188 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.18,
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,
)...