...

View Full Version : How to change 'class' in <A> dynamically ?



meterlink
07-09-2002, 10:18 PM
Hello,
I have this structure :
<a href="xxx" class="menu"> .... </a>
How to find the object of this <a> tag from javascript and
how to change the class of this <a> tag dynamically from javascript?

thank you very much

adios
07-09-2002, 10:23 PM
<html>
<head>
<title>untitled</title>
<style type="text/css">

.oldclass {
color: #00ff00;
}

.newclass {
color: #ff0000;
}

</style>
</head>
<body>
<a id="a1" class="oldclass" href="#">test</a><br><br>
<a href="#"
onclick="document.getElementById('a1').className='newclass'">change class</a>
</body>
</html>

drecksau
11-02-2004, 06:13 PM
I am trying to change the color of text using Classes on specific row or rows depending on the month. There are two rows that are numbered id="11". Only the first row with id="11" is changed. What gives...?

Please look at the following script:

<script>
now = new Date();
month = now.getMonth();

function SetStyle(){
document.getElementById(month).className='newclass'
}
</script>

<table>
<tr id="10">
<td>
Blah Blah Blah
</td>
</tr>
<tr id="11">
<td>
Blah Blah Blah
</td>
<td>
Blah Blah Blah
</td>
</tr>
<tr id="11">
<td>
Blah Blah Blah
</td>
<td>
Blah Blah Blah
</td>
</tr>
</table>

Puffin the Erb
11-02-2004, 07:27 PM
getElementById is intended to reference uniquely.
Amend your SetStyle function as follows so that you can reference by array :

function SetStyle(){

var totalTRs = document.getElementsByTagName("tr").length;

for(i=0; i < totalTRs; i++)
{
document.getElementsByTagName("tr")[i].id == month ?
document.getElementsByTagName("tr")[i].className='newclass' : "";
}
}

drecksau
11-02-2004, 07:57 PM
getElementById is intended to reference uniquely.
Amend your SetStyle function as follows so that you can reference by array :

function SetStyle(){

var totalTRs = document.getElementsByTagName("tr").length;

for(i=0; i < totalTRs; i++)
{
document.getElementsByTagName("tr")[i].id == month ?
document.getElementsByTagName("tr")[i].className='newclass' : "";
}
}

You are the man... Thanks a bunch.


Drecksau

...it said, "Windows 2000 or better." So I installed Linux...

adios
11-02-2004, 08:01 PM
:eek:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum