Forum Moderators: open

Message Too Old, No Replies

Simple problem involving colours

javascript

         

Wurlitzer

5:48 pm on Apr 27, 2006 (gmt 0)

10+ Year Member



Hi,
I borrowed some code from this thread [webmasterworld.com] as it appeared to suit my needs exactly.
As I'm a newbie to programming in general, it's the only way for me to get things done right now!

It is a clickable box which changes color and sends a hidden value upon submit.
My problem is that I need to change the background-colors "green" and "red" to hex values like #f30 and #cf0, but it messes up the behavior of the box. I seem to have run out of ideas!

Any help would be much appreciated.
Thank you.

Here's the code :


<html>
<head>
<title>testing</title>
<script type="text/javascript">
function toggleColor(obj,chkID,val)
{
var chk = document.getElementById(chkID);

if (obj.style.backgroundColor == "red")
{
obj.style.backgroundColor = "green";
chk.value = "";
}
else
{
obj.style.backgroundColor = "red";
chk.value = "RED";
}
}
</script>
</head>
<body>

<div style="background-color:green; width: 50px; height: 50px;" onclick="toggleColor(this,'hdnCheckValue1')"></div>

<form method="post" action="<?php echo $PHP_SELF;?>">
<input type="hidden" id="hdnCheckValue1" name="hdnCheckValue1" value="GREEN"/>
<input type="submit" name="submit" value="OK" />
</form>

<? echo $_POST['hdnCheckValue1'];?>

</body>
</html>

whoisgregg

11:37 pm on Apr 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This probably has something to do with browsers defining colors internally in different ways. Some browsers store the hexadecimal #000000 as rgb(0, 0, 0) so you can't grab it as if(style.color=='#000000')

What I would do is use a className as a flag instead of modifying and testing the color value directly. Then let CSS handle the coloring. :)

If you want sample code, I can probably whip some up, but you can probably get the gist of what should be done.

Wurlitzer

12:01 pm on Apr 28, 2006 (gmt 0)

10+ Year Member



Thank you!
Here's what I did :

<style type="text/css">
.red {background-color:#ff0000;}
.green {background-color:#00ff00;}
</style>

<script type="text/javascript">
function toggleColor(obj,chkID,val)
{
var chk = document.getElementById(chkID);

if (obj.className == "red")
{
obj.className = "green";
chk.value = "";
obj.set=true;
}
else
{
obj.className = "red";
chk.value = "RED";
obj.set=false;
}
}