View Full Version : getting elements and applying stuff to them
scroots
01-28-2003, 07:49 PM
if i have the following code
document.getElementById("1").style.color="rgb("+hex+","+hex+","+hex+")";
the above code resides in the follwoing code
hex=255 // Initial color value.
function fadetext(){
if(hex>0) { //If color is not black yet
hex-=11; // increase color darkness
document.getElementById("1").style.color="rgb("+hex+","+hex+","+hex+")";
//document.getElementsByTagName("link").style.color="rgb("+hex+","+hex+","+hex+")";
setTimeout("fadetext()",20);
}
else
hex=255 //reset hex value
}
how can i apply it to all the elements with the ID of 1? Or how can i apply it to all hyperlinks?
is there away?
scroots
Danne
01-28-2003, 08:45 PM
Do you want to fade all elements with a certain id?
if the id is "idA":
hex=255 // Initial color value.
function fadetext(){
var i;
if(hex>0) { //If color is not black yet
hex-=11; // increase color darkness
for(i=0;i<idA.length;i++)
idA[i].style.color="rgb("+hex+","+hex+","+hex+")";
setTimeout("fadetext()",20);
}
else
hex=255; //reset hex value
}
I changed the id, since variables can't start with a number...:)
scroots
01-28-2003, 08:57 PM
Do you want to fade all elements with a certain id?
Yes that is what i would like to do, using your code I get idA is undefined, in my code for html i have id="idA"
scroots
beetle
01-28-2003, 09:00 PM
You can use the document.links collection.
BTW, your variable hex is a bit misleading, as that isn't a hex value
scroots
01-28-2003, 09:08 PM
it is from a script, i haven`t changed it, i understand it might be misleading.
scroots
Danne
01-28-2003, 09:09 PM
You need at least two elements with that id, for it to become a collection. You could check for the length property if you don't know.
Anyway, using that document.links collection seems easier in this case..:)
scroots
01-28-2003, 09:18 PM
beetle how would i go about using the document.links collection?
scroots
beetle
01-28-2003, 09:38 PM
Like thisvar hex = "FF";
function fadetext()
{
hex = parseInt( "0x" + hex );
if ( hex > 0 ) //If color is not black yet
{
hex = ( hex - 17 ).toString( 16 );
for ( var i = 0; ( link = document.links[i] ); i++ )
link.style.color = "#" + hex + hex + hex;
setTimeout("fadetext()",20);
}
else
hex = "FF"; //reset hex value
}Of course, I'm not sure why you'd want to reset the hex value at the end...
scroots
01-28-2003, 09:59 PM
thanks beetle it has helped me understand and expand my knlowedge.
scroots
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.