473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

javascript button to toggle document background colour

hi all,
i need to create a button with javascript, that when i click on it, it will change the background color from white to black and backwards. I mean, every time i hit it, change the colo between white and black. Can you please show me the javascript for that?
Nov 17 '11 #1
11 7143
Rabbit
12,516 Expert Mod 8TB
What do you have so far?
Nov 17 '11 #2
i have code to change between 2 background colors onclick. It is the background
color of the body of the webpage i want to change.
the code is this:

Expand|Select|Wrap|Line Numbers
  1. function changeBackgroundColor()
  2.         {
  3.             var backColor = new String();
  4.  
  5.             backColor = document.getElementById().style.backgroundColor;
  6.  
  7.                         if(backColor.toLowerCase()=='#e2e2e2' || backColor.toLowerCase()=='rgb(227, 227, 227)')
  8.             {
  9.                 document.getElementById().style.backgroundColor = '#000000';
  10.             }
  11.             else
  12.             {
  13.                 document.getElementById().style.backgroundColor = '#e2e2e2';
  14.             }
  15.         }
Nov 17 '11 #3
Actually, the whole project is like this: I want to have a button(or whatever object in a web page, it's not important right now) that when i click on it, it will change both the header's bg image and the body's background color and also work backwards.



code so far:

Change body bg color:

Expand|Select|Wrap|Line Numbers
  1. function changeBackgroundColor()
  2.         {
  3.             var backColor = new String();
  4.  
  5.             backColor = document.getElementById("body").style.backgroundColor;
  6.             if(backColor.toLowerCase("body")=='#e2e2e2' || backColor.toLowerCase("body")=='rgb(227, 227, 227)')
  7.             {
  8.                 document.getElementById("body").style.backgroundColor = '#000000';
  9.             }
  10.             else
  11.             {
  12.                 document.getElementById("body").style.backgroundColor = '#e2e2e2';
  13.             }
  14.         }
  15.  
Change header image(this one changes image once, but i want to to work backwards too)

Expand|Select|Wrap|Line Numbers
  1.     function changeHeaderImage()
  2.     {
  3.         var imgPath = new String();
  4.         imgPath = document.getElementById("header").style.backgroundImage;
  5.  
  6.         if(imgPath == "url(images/header_bg.jpg)" || imgPath == "")
  7.         {
  8.             document.getElementById("header").style.backgroundImage = "url(images/header_black.jpg)";
  9.         }
  10.         else
  11.         {
  12.             document.getElementById("header").style.backgroundImage = "url(images/header_bg.jpg)";
  13.         }
  14.     }
  15.  
thanks for your help.
Nov 17 '11 #4
Rabbit
12,516 Expert Mod 8TB
So basically you just want to call both functions? Why not do just that?
Nov 17 '11 #5
Because they don't work. Until now, i only succeded in changing the header's bg image and it doesn't work backwards.
Nov 17 '11 #6
Rabbit
12,516 Expert Mod 8TB
Well, first off, if you're trying to change the background color of the body, you use document.bgColor. As for the background image, I have no problem running that code. Other than your extraneous space between background and Image, it worked fine. But I assume that's a transposition error, otherwise it wouldn't run even the first time.
Nov 17 '11 #7
What i meant saying backwards, is that i want to change the image (or color) every time i click on the button, not just twice. Sorry my english is not that good.
Nov 17 '11 #8
Rabbit
12,516 Expert Mod 8TB
I know what you mean. It works for me.
Nov 17 '11 #9
It didn't work for me for some reason, so i used a script that changes css class and it worked. I have this code above to change (toggle is the word i was looking for) the document bg color, but i can't get it to work.


Expand|Select|Wrap|Line Numbers
  1. function changeBackgroundColor()
  2.         {
  3.             var backColor = new String();
  4.  
  5.             backColor = document.backgroundColor;
  6.             if(backColor.toLowerCase()=='#e2e2e2' || backColor.toLowerCase()=='rgb(227, 227, 227)')
  7.             {
  8.                 document.backgroundColor = '#0f0f0f';
  9.             }
  10.             else
  11.             {
  12.                 document.backgroundColor = '#e2e2e2';
  13.             }
  14.         }
Nov 18 '11 #10
I changed the previous script to this


Expand|Select|Wrap|Line Numbers
  1. function changeBackgroundColor()
  2.         {
  3.             var backColor = new String();
  4.             backColor = document.body.style.backgroundColor;
  5.             if(backColor.toLowerCase()=='#0f0f0f' || backColor.toLowerCase()=='rgb(15, 15, 15)')
  6.             {
  7.                 document.body.style.backgroundColor = '#ffffff';
  8.             }
  9.             else
  10.             {
  11.                 document.body.style.backgroundColor = '#0f0f0f';
  12.             }
  13.         }
it worked. thanks
Nov 18 '11 #11
omerbutt
638 512MB
hi , a better approach is to use css selectors / classes to assign different color schemes and then you can switch between them using javascript


To add a class to an element:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("MyElement").className += " MyClass";
  2.  
To remove a class from an element:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("MyElement").className = 
  2.    document.getElementById("MyElement").className.replace
  3.       ( /(?:^|\s)MyClass(?!\S)/ , '' )
  4. /* code wrapped for readability - above is all one statement */
  5.  
To do that in an onclick event:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     function changeClass()
  3.     {
  4.         // code examples from above
  5.     }
  6. </script>
  7. ...
  8. <button onclick="changeClass()">My Button</button>
  9.  
Better yet, use a framework (in this example jQuery) which allows you to do the following:
Expand|Select|Wrap|Line Numbers
  1. $j('#MyElement').addClass('MyClass');
  2.  
  3. $j('#MyElement').removeClass('MyClass');
  4.  
  5. $j('#MyElement').toggleClass('MyClass');
  6.  
And also:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     function changeClass()
  3.     {
  4.         // code examples from above
  5.     }
  6.  
  7.     $j(':button:contains(My Button)').click(changeClass);
  8. </script>
  9. ...
  10. <button>My Button</button>
  11.  
regards
Omer Aslam
Nov 27 '11 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: John D | last post by:
I am trying to change the background colour of either a div or a textarea with javascript, but am having problems. I have passed a hex colour value in the variable hval, and with the following...
1
by: Alistair Birch | last post by:
Hi I want rows of a table to appear in alternating background colours. Having looked around the web I can't find any solution apart from waiting for the next version of CSS, so I tried building...
4
by: Dj Frenzy | last post by:
Hi, I know how to use javascript to change a background image to another background image, and how to change a background colour to another background colour. Is there a way to change an image to a...
2
by: Martin | last post by:
Dear Experts! I'd be grateful if you can help me with this. I'm having an inline frame which points to 'www.externalsite.com'. The page which contains the inline frame has an orange...
4
by: david.graham18 | last post by:
Hi I spotted some nice code to change the background colour of a web page to one of four different colours at random but I can't find it now! The method was to select a number at random from 1...
3
by: Peter Williams | last post by:
Hi All, I want to write some javascript for a html page which does the following. Imagine that the page contains a table with 2 columns and 3 rows, e.g.: +---+---+ | A | B | +---+---+
4
by: Lachlan Hunt | last post by:
Hi, I'm looking for an interoperable method of determining the current background colour of an element that is set using an external stylesheet. I've tried: elmt.style.backgroundColor; but...
6
by: Piyu | last post by:
Hi, I am working in jsp pages.in that i used background colour for heading through css.and those colours(heading, image background colour also) are not coming in printing. from my machine i...
3
by: Yashesh Bhatia | last post by:
Hi Sorry if it's a trivial question. I'm a newbie to js and did some reading since the last 3-4 hrs but have not been able to get the answers, hence this post. essentially i'm trying to...
3
by: bettyboo | last post by:
Hi I'm new to the forum and also a VERY new user of Access to develop databases. I'm building a DB for a driving instructor acquaintance, and he wants a button on the pupil data entry form which...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.