Solved

Simple Javascript problem

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

Assisted Solution

by:Stefan Hoffmann
Stefan Hoffmann earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Glad that I could help.  Thanks for the points.
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Just keep in mind, that HTML5 is also about semantic web..
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
jquery, dropdown 4 33
Angular - (very close) following an example 20 29
Bootstrap input box width 2 12
HTML CSS 7 14
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
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…

763 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

9 Experts available now in Live!

Get 1:1 Help Now