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
Solved

Simple Javascript problem

Posted on 2015-02-18
5
121 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
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 custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

856 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