Solved

Simple Javascript problem

Posted on 2015-02-18
5
118 Views
Last Modified: 2015-02-19
This code fails the if statement when it should not and I don't know why

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style>
        #Button1 {
            background: #00aa58;
        }

    </style>
</head>

<body>
    <input id="Button1" type="button" value="Toggle Color" onclick="toggleColor()" />
    <script language="javascript" type="text/javascript">
        function toggleColor() {
            if (document.getElementById("Button1").style.background == "#00aa58") {
                document.getElementById("Button1").style.background = "#ee3424";
            }
            else {document.getElementById("Button1").style.background = "#00aa58";}
        }

    </script>
</body>
</html>
0
Comment
Question by:BobHavertyComh
  • 2
  • 2
5 Comments
 
LVL 33

Assisted Solution

by:ste5an
ste5an earned 150 total points
ID: 40616941
It's tricky to query colors.. instead of it use your own data attribute to get a more semantically solution:

<!DOCTYPE html>
<html>
	<head>
		<title></title>
		<style>#Button1 { background: #00aa58; }</style>
	</head>
	<body>
		<input id="Button1" data-state="active" type="button" value="Toggle Color" onclick="toggleColor()" />
		<script language="javascript" type="text/javascript">
			function toggleColor() {			
				var button = document.getElementById("Button1");			
				if (button.dataset.state === "active") {
					button.dataset.state = "inactive";
					button.style.backgroundColor = "#EE3424";				 
				}
				else {
					button.dataset.state = "active";
					button.style.backgroundColor = "#00AA58";
				}
				console.log(button.dataset.state);
			}
		</script>
	</body>
</html> 

Open in new window

0
 
LVL 9

Accepted Solution

by:
Brian Tao earned 350 total points
ID: 40616975
Actually you've got 2 issues causing the code not to work:
1. In some browsers, for example Firefox, using "background" will give you all the background properties including color, image, repeat, attachment.... and so on, so you should use "backgroundColor".
2. In most (if not all) browsers, javascript translates #rrggbb representations to rgb(r, g, b) format, so you'll never get a match in your if statment.

Please use the following code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style>
        #Button1 {
            background-color: rgb(0, 170, 88);
        }

    </style>
</head>

<body>
    <input id="Button1" type="button" value="Toggle Color" onclick="toggleColor()" />
    <script language="javascript" type="text/javascript">
        function toggleColor() {
            if (document.getElementById("Button1").style.backgroundColor == "rgb(0, 170, 88)") {
                document.getElementById("Button1").style.backgroundColor = "rgb(238, 52, 36)";
            }
            else {document.getElementById("Button1").style.backgroundColor = "rgb(0, 170, 88)";}
        }

    </script>
</body>
</html>

Open in new window

0
 
LVL 9

Author Closing Comment

by:BobHavertyComh
ID: 40617068
Both were good and true answers and very well explained but the code that I awarded 350 to was a more simple solution. Thanks guys.
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40618532
Glad that I could help.  Thanks for the points.
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40618546
Just keep in mind, that HTML5 is also about semantic web..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now