JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 13th, 2013, 05:47 PM
Inordinate Inordinate is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 Inordinate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 10 m 4 sec
Reputation Power: 0
Other - Can't change background color

I'm trying to make a function which will change the background color of a page from a dropdown menu.
I'm not sure why it doesn't work; any help or tips would be great.

This is the function. I don't know where or what's wrong with it.

PHP Code:
<script type "text/javascript">
        function 
bgChanger()
        {
            
defaultcolor window.document.bgColor;
            
backcolorvalue window.document.bgchanger.bg.selectedIndex;
            
backcolor window.document.bgchanger.bg[backcolorvalue].value;
            if (
backcolorvalue == 0)
            {
                
window.document.bgcolor '#0040FF';
            }
            else if (
backcolorvalue == 1)
            {
                
window.document.bgcolor '#CC00FF';
            }
            else if (
backcolorvalue == 2)
            {
                
window.document.bgcolor '#CCFFCC';
            }
            else if (
backcolorvalue == 3)
            {
                
window.document.bgcolor '#FFFF33';
            }
            else if (
backcolorvalue == 4)
            {
                
window.document.bgcolor defaultcolor;
            }
        }
        
</script> 


Here's the form.

PHP Code:
<form name "bgchanger">
<
select name "bg" onChange "bgChanger()">
<
option value "blue">Blue</option>
<
option value "red">Red</option>
<
option value "green">Green</option>
<
option value "yellow">Yellow</option>
<
option value "default">Default</option>
</
select>
</
form

Reply With Quote
  #2  
Old January 13th, 2013, 06:17 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,881 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 8 h 22 m
Reputation Power: 813
Hi,

it's
Code:
document.bgColor

with a big "C" (like you wrote in the first few lines of the function). JavaScript is case sensitive.

Reply With Quote
  #3  
Old January 13th, 2013, 07:28 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
Welcome to DevShed Forums, Inordinate.

Jacques1, you're not going to mention that it's deprecated and the CSS background-color property should be used instead?

In JavaScript, hyphens aren't allowed in variable names, so "camel-case" is used instead, therefore, "background-color" becomes "backgroundColor":
Code:
document.body.style.backgroundColor
Comments on this post
Jacques1 agrees: You're absolutely right about the deprecation. To be honest, I didn't know document.bgColor even
exists...
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

Remember people spend most of their time on other people's sites (so don't violate web design conventions).

Reply With Quote
  #4  
Old January 14th, 2013, 02:29 PM
Inordinate Inordinate is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 Inordinate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 10 m 4 sec
Reputation Power: 0
Jacques1: Thank you very much. I probably wouldn't have been able to figure this out on my own.

Kravvitz: I don't know any CSS right now so I might just be sticking with what I'm using D:
Thanks for letting me know though.

Reply With Quote
  #5  
Old January 14th, 2013, 03:29 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
All right, consider this your first lesson. The "background-color" property takes the same values as the old bgColor attribute (plus many more).

For example, you can set the background to blue with either of these:
Code:
document.body.style.backgroundColor = '#0000ff';
document.body.style.backgroundColor = 'blue';
Comments on this post
web_loone08 agrees: I like to use the DOM method; when I change the style of elements, because I think the above method
may be depreciated, in the future. I change the background color; like so:
document.getElementsByTagName("body")[0].style.backgroundColor = "#0000ff";

Reply With Quote
  #6  
Old January 14th, 2013, 09:49 PM
Inordinate Inordinate is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 Inordinate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 10 m 4 sec
Reputation Power: 0
Quote:
Originally Posted by Kravvitz
All right, consider this your first lesson. The "background-color" property takes the same values as the old bgColor attribute (plus many more).

For example, you can set the background to blue with either of these:
Code:
document.body.style.backgroundColor = '#0000ff';
document.body.style.backgroundColor = 'blue';


So basically I should just replace bgColor with backgroundColor?

Reply With Quote
  #7  
Old January 14th, 2013, 09:54 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 605 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 16 h 34 m 55 sec
Reputation Power: 69
That would be the best coding practices; especially if your just learning JavaScript. That way you do not have to break bad coding habit, later on.

Reply With Quote
  #8  
Old January 14th, 2013, 11:20 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
Yes, replace "window.document.bgColor" with "document.body.style.backgroundColor". The "window.", in your original code, would only be necessary if you had a local variable named "document".

Quote:
Originally Posted by web_loone08
I like to use the DOM method; when I change the style of elements, because I think the above method
may be depreciated, in the future. I change the background color; like so:
document.getElementsByTagName("body")[0].style.backgroundColor = "#0000ff";

Yes, "document.body" is the old way, but I don't see it going away any time soon and it's so much shorter than using getElementsByTagName(). Both are part of the DOM2 standards.
http://www.w3.org/TR/DOM-Level-2-HT...l#ID-1006298752
http://www.w3.org/TR/DOM-Level-2-Co...html#i-Document
Comments on this post
web_loone08 agrees: I guess only time will tell, but usually syntax is easy; get's depreciated in later browser versions
(example: bgColor).

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Other - Can't change background color

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap