Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Cross-browser way to retrieve styles for elements via the DOM (See related posts)

document.getElementById('whatever').style only gives local styles, but if you want those specified in external CSS definitions, you have to arse about (why, oh, why?) and do it like this:

function newGetStyle(nodeName, sStyle) {
	var x = document.getElementById(nodeName);
	var y;
	if (x.currentStyle) {
		y = x.currentStyle[sStyle];
	} else {
		try {
		y = document.defaultView.getComputedStyle(x,null).getPropertyValue(sStyle);
	  } catch(e) { }
	}
	return y;
}

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts