Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 127
  • Last Modified:

Simple Javascript problem

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
BobHavertyComh
Asked:
BobHavertyComh
  • 2
  • 2
2 Solutions
 
ste5anSenior DeveloperCommented:
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
 
Brian TaoSenior Business Solutions ConsultantCommented:
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
 
BobHavertyComhAuthor Commented:
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
 
Brian TaoSenior Business Solutions ConsultantCommented:
Glad that I could help.  Thanks for the points.
0
 
ste5anSenior DeveloperCommented:
Just keep in mind, that HTML5 is also about semantic web..
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now