Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get CSS style value?

Status
Not open for further replies.

jaschulz

Programmer
May 20, 2005
89
FR
Given the following CSS:

#centerCol {
padding: 5px 0px 5px 0px;
}

does this:

oCenterCol = document.getElementById('centerCol');
alert(oCenterCol.style.paddingTop);

show no error, but an empty alert dialog?

Thanks,

JAS
 
You have to kind of "hack" it (only works in newer browsers).

Firefox/Opera:
Code:
var center = document.getElementById("centerCol");
var padTop = document.defaultView.getComputedStyle(center,null).paddingTop;
alert(padTop);

IE:
Code:
var center = document.getElementById("centerCol").currentStyle.paddingTop;
alert(center);

Ron Wheeler
 
I found this on the O'Reilly site:

function getElementStyle(elemID, IEStyleProp, CSSStyleProp) {
var elem = document.getElementById(elemID);
if (elem.currentStyle) {
return elem.currentStyle[IEStyleProp];
} else if (window.getComputedStyle) {
var compStyle = window.getComputedStyle(elem, "");
return compStyle.getPropertyValue(CSSStyleProp);
}
return "";
}

It works.

Thanks,

JAS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top