/    Sign up×
Community /Pin to ProfileBookmark

Determine attributes of css class via javascript

Hey everyone,

I assign a CSS class to a div element using javascript:

[CODE]page_div.setAttribute(“class”, page_class);[/CODE]

How can I access the different members of the CSS class (e.g. width, height) using JS?

I tried

[CODE]page_div.style.height[/CODE]

but unfortunately it says “undefined”

Looking forward to any advice

Steve

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsJul 04.2012 — [CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<style type="text/css">
/*<![CDATA[*/
.test {
height:400px;
background-Color:red;
}

/*]]>*/
</style></head>

<body>
<div class="test" id="tst" >1</div>

<script type="text/javascript">
/*<![CDATA[*/
function style(o,p){
if (o.currentStyle){
return o.currentStyle[p.replace(/-/g,'')];
}
return document.defaultView.getComputedStyle(o,null).getPropertyValue(p.toLowerCase());
}

alert(style(document.getElementById('tst'),'height'));
alert(style(document.getElementById('tst'),'background-Color'));

/*]]>*/
</script>


</body>

</html>[/CODE]
Copy linkTweet thisAlerts:
@pro_ceed1authorJul 05.2012 — Problem solved.

Thanks a lot :-)
×

Success!

Help @pro_ceed1 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.19,
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,
)...