HighDots Forums  

Color swap

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss Color swap in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
M
 
Posts: n/a

Default Color swap - 06-05-2006 , 02:40 PM






Hello,

I would like to swap the color of a text item when a js function is called.

---------------------------------------------
function swap(id) {

green = '#00f000';
red = '#f00006';

if (document.getElementById(id).style.color == green)
document.getElementById(id).style.color = red;
else
document.getElementById(id).style.color = green;

}
-----------------------------------------------

This looks easy, doesn't it ? The affectation is OK :
document.getElementById(id).style.color = green; // This works

But the test doesn't :
if (document.getElementById(id).style.color == green) // alaways false

When I try :
alert(document.getElementById(id).style.color)
I got an empty string...

I don't know how to get the color value.
Thank you for your help.

M

Reply With Quote
  #2  
Old   
Wayne Dobson
 
Posts: n/a

Default Re: Color swap - 06-05-2006 , 06:41 PM






<M> wrote

Quote:
Hello,

I would like to swap the color of a text item when a js function is
called.

---------------------------------------------
function swap(id) {

green = '#00f000';
red = '#f00006';

if (document.getElementById(id).style.color == green)
document.getElementById(id).style.color = red;
else
document.getElementById(id).style.color = green;

}
-----------------------------------------------

This looks easy, doesn't it ? The affectation is OK :
document.getElementById(id).style.color = green; // This works

But the test doesn't :
if (document.getElementById(id).style.color == green) // alaways false
I can't tell why that would be, without an example of the code failing or
knowing which browser you ran the code in.

Quote:
When I try :
alert(document.getElementById(id).style.color)
I got an empty string...
That's what it is, before it's initialised.

Quote:
I don't know how to get the color value.
Thank you for your help.
How you claim to have to have done it is along the right lines.

--
Wayne
AKA "Dobbie the House Elf"




Reply With Quote
  #3  
Old   
M
 
Posts: n/a

Default Re: Color swap - 06-06-2006 , 05:16 PM



Well,

I did it work. Here is the result :

function swap(id) {

red = "rgb(240,0,6)";
green = "rgb(0,240,0)";


//alert(document.getElementById(id).style.color);

// I get the colour and delete space char
// for IE, colour is : rgb(r,g,b)
// for Firefox, colour is : rgb(r, g, b)
// Note the space after coma !
color = document.getElementById(id).style.color;
color = couleur.replace(/\s/g,"");

if (color == green)
document.getElementById(id).style.color = red;
else
document.getElementById(id).style.color = green;

}
----------------------------------------------------
In the HTML page:
<span id="test" style="color:#f00006"
onclick="javascript:swap('test');">My text</span>
-------------------------------------------------------

Reply With Quote
Reply




Thread Tools
Display Modes

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

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



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.