Solved

Simple Javascript problem

Posted on 2015-02-18
5
123 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 34

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 34

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

710 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