Forum Moderators: open

Message Too Old, No Replies

onclick add a classname to a TAG

adding classname to a TAG

         

sugar2

12:45 am on May 18, 2005 (gmt 0)

10+ Year Member



this topic looks like one in the past, i know but that topic does not allow replies...

im trying to add a class name in a exsisting TD tag of a HTML table.

this code doesnt print any errors but dont add the class either...

<SCRIPT>
function changeclass() {
var NAME = document.getElementsByTagName("TD")
NAME.className="items"
}
</SCRIPT>

<a href="#" onclick="changeclass();">TEST</a>

and this is my table sample wich have no class asigned, it is waiting for the script who is supposed will add it a neww class

<style>
TD.items {COLOR: 000;}
TD.items A:link {COLOR: blue; font-weight: bold;}
TD.items A:visited {COLOR: blue; font-weight: bold;}
TD.items A:active {COLOR: blue; font-weight: bold;}
TD.items A:hover {COLOR: #505C6D; font-weight: bold;}
</style>
<table>
<tr>
<td><a href="http://somewhere.com"><b>my custom text</a></td>
</tr>
</table>

any help will be apreciated

thanks

Bernard Marx

1:20 am on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



document.getElementsByTagName doesn't return a "tag", it returns a collection of elements.
You can't set a className on a collection of elements all in one go. You need to iterate.

See:http://www.webmasterworld.com/forum91/3730.htm

It's probably easier in your case to switch the class of the table, and getthe effect by using a descendant selector in the stylesheet.

-----

Unless you mean that there's only one TD in the whole document:

document.getElementsByTagName('td')[0].className = 'foo';

sugar2

9:31 pm on May 18, 2005 (gmt 0)

10+ Year Member



[php]<SCRIPT>
function changeclass() {
var name=document.getElementsByTagName('td');
for (var i=0; i<name.length; i++)
{
name[i].className='items';
}
}
</SCRIPT>
<a href="#" onclick="changeclass();">TEST</a>[/php]

thanks... after read your post i understand a couple things..

it was fiexed by adding a for loop base in the concept of may td's

thnaks