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

About this user

Peter Cooperx http://www.petercooper.co.uk/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Cross-browser way to retrieve styles for elements via the DOM

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:

   1  
   2  function newGetStyle(nodeName, sStyle) {
   3  	var x = document.getElementById(nodeName);
   4  	var y;
   5  	if (x.currentStyle) {
   6  		y = x.currentStyle[sStyle];
   7  	} else {
   8  		try {
   9  		y = document.defaultView.getComputedStyle(x,null).getPropertyValue(sStyle);
  10  	  } catch(e) { }
  11  	}
  12  	return y;
  13  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS