Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-13-2012, 04:49 PM   PM User | #1
jigneshjsoni
New Coder

 
Join Date: Mar 2012
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
jigneshjsoni is an unknown quantity at this point
function error

Code:
<html>
	<head>
		<script>


		function changeBgColor(){
						document.getElementById("bgc").style.background-color=333333;

		}
		</script>

		<style>
			#bgc{
				width:300px;
				height:100px;
				background-color:#cccccc;
			}
		</style>
	</head>

	<body>
		<p  id="bgc" onmouseover="changeBgColor()">
			Hi There, pls change bg color
		</p>

	</body>
</html>
I am getting following error

"SCRIPT5007: The value of the property 'changeBgColor' is null or undefined, not a Function object eventTest1.html, line 22 character 29"

This is a simple code, why am I getting such a weird error. Whats wrong in here ?

Thanks
jigneshjsoni is offline   Reply With Quote
Old 04-13-2012, 04:58 PM   PM User | #2
M.Jackson
Regular Coder

 
Join Date: Aug 2011
Posts: 120
Thanks: 1
Thanked 15 Times in 15 Posts
M.Jackson is an unknown quantity at this point
Your function is trying to assign a number to something that is not a number...

Code:
<html>
	<head>
		<script>
		function changeBgColor()
			{
			document.getElementById("bgc").style.background='red';
			}
		</script>

		<style>
			#bgc{
				width:300px;
				height:100px;
				background-color:#cccccc;
			}
		</style>
	</head>
	<body>
		<p  id="bgc" onmouseover="changeBgColor()">
			Hi There, pls change bg color
		</p>
	</body>
</html>
if you want to use a more specific color, use the full code ( document.getElementById('').style.color='#333333' ).

Last edited by M.Jackson; 04-13-2012 at 04:58 PM.. Reason: typo
M.Jackson is offline   Reply With Quote
Old 04-13-2012, 10:27 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
document.getElementById("bgc").style.background-color is invalid syntax - you can't subtract color from document.getElementById("bgc").style.background on the left of an assignment.

Where CSS contains a - the corresponding JavaScript drops the - and capitalizes the next letter

document.getElementById("bgc").style.backgroundColor = '#333';
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:28 PM.


Advertisement
Log in to turn off these ads.