471,071 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

Easy change link colors on page w/out reload

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?

Jul 20 '05 #1
9 3799

"michael" <no****@example.com> wrote in message
news:cb*************@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");

Jul 20 '05 #2
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.
Jul 20 '05 #3
In article <cb*************@news.t-online.com>, no****@example.com
says...
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.

Use a for loop on the collection.
Jul 20 '05 #4
> Use a for loop on the collection.

Does anyone have an example?

Jul 20 '05 #5
> 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';
}
Jul 20 '05 #6
On Tue, 22 Jun 2004 18:13:43 +0200, michael <no****@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

Jul 20 '05 #7
> Why?

Why not?
Jul 20 '05 #8
On Wed, 23 Jun 2004 07:51:05 +0200, michael <no****@example.com> wrote:
Why?


Why not?

For starters, not all users have Javascript enabled. You can change link
colors in CSS, which is more widely trusted.
Jul 20 '05 #9
michael wrote:
Use a for loop on the collection.


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/
Jul 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mat | last post: by
4 posts views Thread by Richard Cornford | last post: by
18 posts views Thread by Denny Schierz | last post: by
reply views Thread by menno.abbink | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.