473,387 Members | 1,540 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.

Toggle colour change on click

KeredDrahcir
426 256MB
I have a division and I want to click on it to change the colour to red and click on it again to change the colour back to black.
The code I'm using doesn't seem to work and I can't work out what I'm doing wrong. Can anyone help me?

HTML
Expand|Select|Wrap|Line Numbers
  1. <div style="width: 30px; height: 30px; border-style: solid; border-color: #ffffff; border-width: 2px 0 0 2px; float: left; background-color: #000000;" id="a1" onclick="plan('a1');">&nbsp;</div>
JavaScript
Expand|Select|Wrap|Line Numbers
  1. function plan(id){
  2.     var obj = document.getElementById(id);
  3.     (obj.style.backgroundColor == "#000000") ? obj.style.backgroundColor = "#ff0000" : obj.style.backgroundColor = "#000000";
  4. }
Many thanks.
Aug 3 '13 #1

✓ answered by yolshevsky

This works.
Expand|Select|Wrap|Line Numbers
  1. obj.style.backgroundColor = (obj.style.backgroundColor == "rgb(0, 0, 0)") ? "#ff0000" : "#000000";
  2.  
apparently the background property is not a string.

2 1652
This works.
Expand|Select|Wrap|Line Numbers
  1. obj.style.backgroundColor = (obj.style.backgroundColor == "rgb(0, 0, 0)") ? "#ff0000" : "#000000";
  2.  
apparently the background property is not a string.
Aug 4 '13 #2
KeredDrahcir
426 256MB
Fantastic. Works perfectly. Many thanks.
Aug 4 '13 #3

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

Similar topics

6
by: Louise | last post by:
Hi I have written an HTML pages which does not have any colour specifying tags as far I know. When I view this in an Microsoft internet explorer browser it appears with a white background and...
1
by: JD | last post by:
How do i get rid of the 1st column that appears in a datagrid? I do not want this? Also how do i change the colour of the background of a cell when the user selects it. i.e for the first coloumn...
2
by: Scott | last post by:
Is it possible to have a button change colour OnClick ? Thanks.
4
by: Bob | last post by:
Hi, Is it possible to change the forecolor of a disabled text control. I have a RichText Box that I don't want the user to edit but the Grey on Grey default is hard to read. Is there a way of...
2
by: sathiyamurthy | last post by:
Hi all I need help regarding button colour change in vc++. tell me the steps to implement. also i want to change the button label text size. is there any function for that. Regards sam
23
by: cmcdermo | last post by:
I want to change the colour of a border around an image when a user clicks on the image. Not sure how to go about this, i have tried a few things but it doens't seem to work. here is my image....
0
by: swansea29 | last post by:
Hi, I have created a subform in my database where actions are input, the request has come from my peers that when they complete there relevent action " they click a checkbox" can it change the...
2
by: gvrajkumar | last post by:
Hi We are developing an Ajax based application. In this application the URL is fixed and as user navigates on the application we will change Anchor on the URL so that user can bookmark the url...
1
by: warder21 | last post by:
Hi All, Not sure wether this is a css issue or javascript but here goes. I am trying to make some html <p> tag text highlight (change colour) when hovering over an image that is relevant to...
2
by: Sean Cutting | last post by:
Hi, Please can you help? I would like to build a website where the colour of the site will change to one of the 4 brand colours everytime the site is refreshed. Can this be done? Regards,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.