Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: function error

  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Aug 2011
    Posts
    120
    Thanks
    1
    Thanked 15 Times in 15 Posts
    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

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •