Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-06-2012, 03:20 PM   PM User | #1
Md8^h2nx
New to the CF scene

 
Join Date: Nov 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Md8^h2nx is an unknown quantity at this point
Javascript and hexadecimal

I just started getting into javascript and I'm having some trouble.

I want it to work like this:

Code:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
::selection {
background:none;
}
::-moz-selection {
background:none;
}
body {
margin:0;
font:bold 18px arial;
}
#box {
background:#000;
cursor:pointer;
padding:4px;
text-align:center;
}
</style>
<script>
function toggle() {
var x=document.getElementById("box").style;
if(x.color=="blue") {
x.color="grey";
}
else if(x.color=="grey") {
x.color="green";
}
else {
x.color="blue";
}
}
</script>
</head>

<body>

<div id="box" style="color:blue;" onclick="toggle()">Toggling Colors</div>

</body>

</html>
But I want to be able to use hexadecimal and not just color names so I did this:

Code:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
::selection {
background:none;
}
::-moz-selection {
background:none;
}
body {
margin:0;
font:bold 18px arial;
}
#box {
background:#000;
cursor:pointer;
padding:4px;
text-align:center;
}
</style>
<script>
function toggle() {
var x=document.getElementById("box").style;
if(x.color=="blue") {
x.color="#808080";
}
else if(x.color=="#808080") {
x.color="green";
}
else {
x.color="blue";
}
}
</script>
</head>

<body>

<div id="box" style="color:blue;" onclick="toggle()">Toggling Colors</div>

</body>

</html>
It doesn't work, but is there a way to make it work.
Md8^h2nx is offline   Reply With Quote
Old 11-07-2012, 12:09 AM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
Here's a little help on trouble shooting:
Your code snippet:
Code:
function toggle() {
var x=document.getElementById("box").style;
if(x.color=="blue") {
x.color="#808080";
}

Read more at http://www.codingforums.com/showthread.php?p=1289328#PgYovAJysFTqm7fQ.99
Right after the first line
Code:
var x=document.getElementById("box").style;
Put
Code:
alert(x);die;
you will get exactly what you x is = [object CSSStyleDeclaration]

Not what you wanted. So you look things up and use
Code:
var x = document.getElementById("box").style.backgroundColor;
This time the alert says blue. Hurray!!

Here's the replacement for this section:
Code:
var x = document.getElementById("box").style.backgroundColor;
if(x == "blue")
document.getElementById("box").style.backgroundColor = "#808080";
}
If you use the alert trick again you will find that #808080 will give you an alert of rgb(128, 128, 128). Not what you wanted, AGAIN. So I found this function here http://haacked.com/archive/2009/12/2...gb-to-hex.aspx and added it.

Now I run it and again have a problem. it wont go to blue from green. So I change the order of the statements. Here is the final results with two functions.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript">
function colorToHex(color) {
    if (color.substr(0, 1) === '#') {
        return color;
    }
    var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);

    var red = parseInt(digits[2]);
    var green = parseInt(digits[3]);
    var blue = parseInt(digits[4]);

    var rgb = blue | (green << 8) | (red << 16);
    return digits[1] + '#' + rgb.toString(16);
};
function toggle()
{
	var x = document.getElementById("box").style.backgroundColor;
	if(x == "green")
	{
		document.getElementById("box").style.backgroundColor = "blue";
	}
	if(x == "blue")
	{
		document.getElementById("box").style.backgroundColor = "#808080";
	}
	if(colorToHex(x) == "#808080")
	{
		document.getElementById("box").style.backgroundColor = "green";
	}

}
</script>
</head>
<body>
<div id="box" style="background-color:blue;height:100px;width:300px;"></div>
<button onclick="toggle()">PUSH</button>
</body>
</html>

Last edited by sunfighter; 11-07-2012 at 12:46 AM..
sunfighter is offline   Reply With Quote
Users who have thanked sunfighter for this post:
Md8^h2nx (11-08-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:14 AM.


Advertisement
Log in to turn off these ads.