You could try to use the non-standard IE element.currentStyle
property, otherwise you can look for the DOM Level 2 standard getComputedStyle
method if available :
function getStyle(el,styleProp) {
var camelize = function (str) {
return str.replace(/\-(\w)/g, function(str, letter){
return letter.toUpperCase();
});
};
if (el.currentStyle) {
return el.currentStyle[camelize(styleProp)];
} else if (document.defaultView && document.defaultView.getComputedStyle) {
return document.defaultView.getComputedStyle(el,null)
.getPropertyValue(styleProp);
} else {
return el.style[camelize(styleProp)];
}
}
Usage:
var element = document.getElementById('elementId');
getStyle(element, 'font-size');
More info:
- Get Styles (QuirksMode)
Edit: Thanks to @Crescent Fresh, @kangax and @Pekka for the comments.
Changes:
- Added
camelize
function, since properties containing hypens, likefont-size
, must be accessed as camelCase (eg.:fontSize
) on thecurrentStyle
IE object. - Checking the existence of
document.defaultView
before accessinggetComputedStyle
. - Added last case, when
el.currentStyle
andgetComputedStyle
are not available, get the inline CSS property viaelement.style
.