An id can be used only once. It can NOT appear in multiple tags.
You need to give each tag a unique id, and then cycle through the ids with a loop in js.
e.g.
<a .... id="aa1">
<a .... id="aa2">
<a .... id="aa3">
<a .... id="aa4">
<a .... id="aa5">
MidiMagic
Nearly a Senior Poster
3,319 posts since Jan 2007
Reputation Points: 730
Solved Threads: 182
Alternatively, try this trick, if you don't mind presetting the available colors ( in a way, it's better because those colours can be defined in the CSS ). This works by dynamically changing the class of the containing element, the CSS 'cascade' effect sorts out the rest:
<html>
<head>
<title>Color changing magic with a classname trick</title>
<style type="text/css">
div.red_links a
{
color:red;
}
div.blue_links a
{
color:blue;
}
</style>
<script type="text/javascript">
function set_linkcolor( to )
{
document.getElementById( "color_state" ).className =
to + "_links";
}
</script>
</head>
<body>
<div id="color_state" class="red_links">
<ul>
<li><a>An anchor</a></li>
<li><a>Another</a></li>
<li><a>And another</a></li>
<li>Some normal text</li>
<li>Some more..</li>
<li>And some more</li>
</ul>
</div>
<span onmousedown="set_linkcolor( 'blue' )"
onmouseup="set_linkcolor( 'red' )"
style="border:solid 1px black;">
Click me!
</span>
</body>
</html>
MattEvans
Veteran Poster
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64
aravelli, that works, thank you, but only for the first one containing the id. On some pages I have more links and now only the first one changes.
this is what I have:
try{document.getElementById('contentbox a').style.color= "#d05048";}catch(err){};
there is a try...cath function because some pages don't have the 'contentbox a' id.
you need to do it this way
var num=document.getElementById("contentbox").childNodes.length;
for(var i=0;i<num;i++){
if(document.getElementById("contentbox").childNodes[i].nodeName=="A"){
document.getElementById("contentbox").childNodes[i].style.color="#d05048";
}
}
essentially you loop through all the elements of div content box for all the links and then you change them.
wrivera
Junior Poster in Training
53 posts since Jan 2010
Reputation Points: 10
Solved Threads: 10