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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a style object

Status
Not open for further replies.

jdbolt

Programmer
Aug 10, 2005
89
CA
I was wondering if tehre was a way to create a style object and the use this to set the styles for a bunch of different components. For example:

Code:
var styleObject = new style();

styleObject.backgroundColor = '#000000';
styleObject.text......

var advancedSearchTable = document.createElement("table");

advancedSearchTable.style = styleObject

I have been playing around with code and havent managed to get anything to work as of yet
 
This works for me in Fx 1.5, IE 6, NN 7, and Opera 8. It did not work in Opera 7.54. I've not tested it on any Mac browsers, or lesser versions of IE.

Code:
<html>
<head>
	<script type="text/javascript">
	<!--

		onload = doCSS;

		function copyStyles(sourceStyles, destEl) {
			for (styleProperty in sourceStyles) {
				var style = sourceStyles[styleProperty];
				if (typeof(style) == 'function' || style == '') continue;
				try {
					destEl.style[styleProperty] = style;
				} catch(e) {}
			}
		}

		function doCSS() {
			var myStyles = document.createElement('div').style;
			myStyles.backgroundColor = '#CC0000';
			myStyles.fontSize = '5em';

			copyStyles(myStyles, document.getElementById('div1'));

			myStyles.backgroundColor = '#0000CC';
			copyStyles(myStyles, document.getElementById('div2'));
		}

	//-->
	</script>
<body>
	<div id="div1">This is Div 1</div>
	<div id="div2">This is Div 2</div>
</body>
</html>

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top