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?
11 7143
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: - function changeBackgroundColor()
-
{
-
var backColor = new String();
-
-
backColor = document.getElementById().style.backgroundColor;
-
-
if(backColor.toLowerCase()=='#e2e2e2' || backColor.toLowerCase()=='rgb(227, 227, 227)')
-
{
-
document.getElementById().style.backgroundColor = '#000000';
-
}
-
else
-
{
-
document.getElementById().style.backgroundColor = '#e2e2e2';
-
}
-
}
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: - function changeBackgroundColor()
-
{
-
var backColor = new String();
-
-
backColor = document.getElementById("body").style.backgroundColor;
-
if(backColor.toLowerCase("body")=='#e2e2e2' || backColor.toLowerCase("body")=='rgb(227, 227, 227)')
-
{
-
document.getElementById("body").style.backgroundColor = '#000000';
-
}
-
else
-
{
-
document.getElementById("body").style.backgroundColor = '#e2e2e2';
-
}
-
}
-
Change header image(this one changes image once, but i want to to work backwards too) -
function changeHeaderImage()
-
{
-
var imgPath = new String();
-
imgPath = document.getElementById("header").style.backgroundImage;
-
-
if(imgPath == "url(images/header_bg.jpg)" || imgPath == "")
-
{
-
document.getElementById("header").style.backgroundImage = "url(images/header_black.jpg)";
-
}
-
else
-
{
-
document.getElementById("header").style.backgroundImage = "url(images/header_bg.jpg)";
-
}
-
}
-
thanks for your help.
So basically you just want to call both functions? Why not do just that?
Because they don't work. Until now, i only succeded in changing the header's bg image and it doesn't work backwards.
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.
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.
I know what you mean. It works for me.
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. - function changeBackgroundColor()
-
{
-
var backColor = new String();
-
-
backColor = document.backgroundColor;
-
if(backColor.toLowerCase()=='#e2e2e2' || backColor.toLowerCase()=='rgb(227, 227, 227)')
-
{
-
document.backgroundColor = '#0f0f0f';
-
}
-
else
-
{
-
document.backgroundColor = '#e2e2e2';
-
}
-
}
I changed the previous script to this - function changeBackgroundColor()
-
{
-
var backColor = new String();
-
backColor = document.body.style.backgroundColor;
-
if(backColor.toLowerCase()=='#0f0f0f' || backColor.toLowerCase()=='rgb(15, 15, 15)')
-
{
-
document.body.style.backgroundColor = '#ffffff';
-
}
-
else
-
{
-
document.body.style.backgroundColor = '#0f0f0f';
-
}
-
}
it worked. thanks
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: -
document.getElementById("MyElement").className += " MyClass";
-
To remove a class from an element: -
document.getElementById("MyElement").className =
-
document.getElementById("MyElement").className.replace
-
( /(?:^|\s)MyClass(?!\S)/ , '' )
-
/* code wrapped for readability - above is all one statement */
-
To do that in an onclick event: -
<script type="text/javascript">
-
function changeClass()
-
{
-
// code examples from above
-
}
-
</script>
-
...
-
<button onclick="changeClass()">My Button</button>
-
Better yet, use a framework (in this example jQuery) which allows you to do the following: -
$j('#MyElement').addClass('MyClass');
-
-
$j('#MyElement').removeClass('MyClass');
-
-
$j('#MyElement').toggleClass('MyClass');
-
And also: -
<script type="text/javascript">
-
function changeClass()
-
{
-
// code examples from above
-
}
-
-
$j(':button:contains(My Button)').click(changeClass);
-
</script>
-
...
-
<button>My Button</button>
-
regards
Omer Aslam
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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 |
+---+---+
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |