This is Interesting: Free Magazines for Graphics designers and webmasters  


Home > Archive > Stylesheets > June 2004 > Easy change link colors on page w/out reload





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Easy change link colors on page w/out reload
michael

2004-06-22, 7:22 pm

I have many links on a page, but for example sake, these two:

<a href="bla1.html" id="link1">bla1</a>
<a href="bla1.html" id="link2">bla1</a>

I use a Javascript function to change the link colors:

document.getElementById("link1").style.color='lime'
document.getElementById("link2").style.color='lime'

Above works fine to change the 2 link colors to lime, but is there way to
dynamically change all links on a page, without a separate id for each
link, and without reloading the page with a new link style declaration?

Harlan Messinger

2004-06-22, 7:22 pm


"michael" <nomail@example.com> wrote in message
news:cb9ma4$2i4$01$1@news.t-online.com...
> I have many links on a page, but for example sake, these two:
>
> <a href="bla1.html" id="link1">bla1</a>
> <a href="bla1.html" id="link2">bla1</a>
>
> I use a Javascript function to change the link colors:
>
> document.getElementById("link1").style.color='lime'
> document.getElementById("link2").style.color='lime'
>
> Above works fine to change the 2 link colors to lime, but is there way to
> dynamically change all links on a page, without a separate id for each
> link, and without reloading the page with a new link style declaration?
>


var links = document.getElementsByTagName("a");

michael

2004-06-22, 7:22 pm

Harlan Messinger wrote:

> var links = document.getElementsByTagName("a");


By the above I've got the variable "links" as an HTML object collection.
I suppose this is now a Javascript question; but how could I dynamically
change all link colors on the page from here?
Any further pointers would be much appreciated... Thanks.


Mr.Clean

2004-06-22, 7:22 pm

In article <cb9pgn$4to$05$1@news.t-online.com>, nomail@example.com
says...
> Harlan Messinger wrote:
>
>
> By the above I've got the variable "links" as an HTML object collection.
> I suppose this is now a Javascript question; but how could I dynamically
> change all link colors on the page from here?
> Any further pointers would be much appreciated... Thanks.
>
>
>

Use a for loop on the collection.
michael

2004-06-22, 7:22 pm

> Use a for loop on the collection.

Does anyone have an example?

michael

2004-06-22, 7:22 pm

> Does anyone have an example?

.... found a solution:

links = document.getElementsByTagName("a");
for (var i=0;i<links.length;i++)
if (links[i].className=="link")
links[i].style.color = 'lime';
}


Jan Roland Eriksson

2004-06-22, 7:23 pm

On Tue, 22 Jun 2004 18:13:43 +0200, michael <nomail@example.com> wrote:

>I have many links on a page, but for example sake, these two:
><a href="bla1.html" id="link1">bla1</a>
><a href="bla1.html" id="link2">bla1</a>
>I use a Javascript function to change the link colors:


Why?

--
Rex

michael

2004-06-23, 4:15 am

> Why?

Why not?
Neal

2004-06-23, 4:15 am

On Wed, 23 Jun 2004 07:51:05 +0200, michael <nomail@example.com> wrote:

>
> Why not?



For starters, not all users have Javascript enabled. You can change link
colors in CSS, which is more widely trusted.
Brian

2004-06-27, 7:16 pm

michael wrote:

>
> Does anyone have an example?


Please take it to comp.lang.javascript. But before posting a question,
check the archives. This has come up before. You might also consider
Google.

--
Brian (remove ".invalid" to email me)
http://www.tsmchughs.com/
Sponsored Links


Copyright 2003 - 2008 forum4designers.com  Software forum  Computer Hardware reviews