By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,708 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

Easy change link colors on page w/out reload

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
> Use a for loop on the collection.

Does anyone have an example?

Jul 20 '05 #5

P: n/a
> 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

P: n/a
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

P: n/a
> Why?

Why not?
Jul 20 '05 #8

P: n/a
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

P: n/a
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.