fade font colour to another

M

Muffinman

Hi,

I'm trying to build a script which will fade the colour of my text to
another. Now I've got here a litte problem. Whene ever I want to replace
one of the numbers in:
document.getElementById("main_txt").style.color = "rgb(0,0,0)";
by a var, something I can control, like: rgb(red, green, blue) of which
I made shure it were integers by parseInt(); I always get the error in
IE that there is an invalid value for the property. I can not figure out
what is wrong about the value. Can anyone give my somebody help me out
on this

Thanks in advance, Maarten

ps.

function change_colour(red, green, blue)
{

dest_red = 202;
dest_green = 207;
dest_blue = 222;

steps_red = (dest_red - red)/100;
steps_green = (dest_green - green)/100;
steps_blue = (dest_green - green)/100;


for(step = 1 ; step <= 100 ; step++)
{

red = parseInt(red);
grr = parseInt(5);
document.getElementById("main_txt").style.color = "rgb(grr,0,0)";

}
}
 
R

RobB

Muffinman said:
Hi,

I'm trying to build a script which will fade the colour of my text to
another. Now I've got here a litte problem. Whene ever I want to replace
one of the numbers in:
document.getElementById("main_txt").style.color = "rgb(0,0,0)";
by a var, something I can control, like: rgb(red, green, blue) of which
I made shure it were integers by parseInt(); I always get the error in
IE that there is an invalid value for the property. I can not figure out
what is wrong about the value. Can anyone give my somebody help me out
on this

Thanks in advance, Maarten

ps.

function change_colour(red, green, blue)
{

dest_red = 202;
dest_green = 207;
dest_blue = 222;

steps_red = (dest_red - red)/100;
steps_green = (dest_green - green)/100;
steps_blue = (dest_green - green)/100;


for(step = 1 ; step <= 100 ; step++)
{

red = parseInt(red);
grr = parseInt(5);
document.getElementById("main_txt").style.color = "rgb(grr,0,0)";

}
}

This:

document.getElementById("main_­txt").style.color = "rgb(grr,0,0)";

....will set the CSS color attribute of that element to - literally -
"rgb(grr,0,0)". To get the variable 'grr' (maybe 'grn' would be better)
to interpolate properly, you'll need to discontinue the quotes to force
it to be evaluated ('looked up') as a variable:

document.getElementById("main_­txt").style.color = "rgb(" + grr +
",0,0)";
 
F

Fred Oz

Muffinman said:
Hi,

I'm trying to build a script which will fade the colour of my text to
another. Now I've got here a litte problem. Whene ever I want to replace
one of the numbers in:
document.getElementById("main_txt").style.color = "rgb(0,0,0)";
by a var, something I can control, like: rgb(red, green, blue) of which
I made shure it were integers by parseInt();

Your use of 'make sure' infers that you are using parseInt() :

1. to validate that the variable is an integer

2. to convert a string to an integer

Both uses are not appropriate. If you are using it as validation,
better methods are suggested here:

<URL:http://www.merlyn.demon.co.uk/js-valid.htm#VNP>

If you are using it to convert a string to a number, see below.
I always get the error in
IE that there is an invalid value for the property. I can not figure out
what is wrong about the value. Can anyone give my somebody help me out
on this

Thanks in advance, Maarten

ps.

function change_colour(red, green, blue)
{

dest_red = 202;
dest_green = 207;
dest_blue = 222;

steps_red = (dest_red - red)/100;
steps_green = (dest_green - green)/100;
steps_blue = (dest_green - green)/100;


for(step = 1 ; step <= 100 ; step++)
{

red = parseInt(red);

You do not provide any code where you actually use 'red', so I'll use
an example from a subsequent line. Remove the parseInt() line above
and do:

...("main_txt").style.color = "rgb(" + +red + ",0,0)";

the '+' character converts 'red' to a number - presuming that you
have already validated that 'red' contains a suitable value (0-255).

In any case, I think it's perfectly OK for 'red' to be a string in
this instance.
grr = parseInt(5);

for grr to be a number:

grr = 5;

does the job as per your earlier use.

Do a search on why parseInt() should always be used with a radix
parameter to discover why its use is discouraged, particularly where
more concise alternatives exist.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,058
Members
48,769
Latest member
Clifft

Latest Threads

Top