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

Why is the color blank? 1

Status
Not open for further replies.

Wolfie7873

Technical User
Jan 15, 2004
94
0
0
US
The Alert box is blank. Why?

Code:
	<script type='text/javascript'>
		function changeColor(x) {
			var mycolor = x.style.backgroundColor;
			alert(mycolor);
			var myDiv = document.getElementById('bigOval');
			myDiv.style.backgroundColor = color;
		}
	</script>
	<body>
		<div class='main-container'>
			<div class='button black-border' id='button1' onclick="changeColor(this)">1</div>
			<div class='button black-border' id='button2' onclick="changeColor(this)">2</div>
			<div class='button black-border' id='button3' onclick="changeColor(this)">3</div>
			<div class='button black-border' id='button4' onclick="changeColor(this)">4</div>
			<div class='button black-border' id='button5' onclick="changeColor(this)">5</div>
			<div class='sub-container black-border' id='bigOval'>
				<div class='content-box'></div>
			</div>
		</div>
	</body>
 
Hi

Because [tt]backgroundColor[/tt] was never set. ( At least not in the code you posted. )

Note that JavaScript can get only the [tt]style[/tt] values it previously set. To access default styles and styles set by CSS, you have to use [tt]getComputedStyle()[/tt]. ( And Microsoft's own property in Explorer. ) To simplify it, use the [tt]getStyle()[/tt] function from Peter-Paul Koch's
Get Styles article.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top