FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

javascript to change table class

 


alalex
i want to change a table class, the table's id is table_container.
here is my current code:
Code:
var table = document.getElementById('table_container').getAttribute('class');
table = 'loaded';

it is suposed to change from class: loading, to loaded...
if anyone could help me with this... Very Happy
Straevaras
Because browsers don't follow standards competely, I find this the best way:
Code:
document.getElementById('table_container').className = 'loaded';
Kaneda
alalex wrote:
i want to change a table class, the table's id is table_container.
here is my current code:
Code:
var table = document.getElementById('table_container').getAttribute('class');
table = 'loaded';

it is suposed to change from class: loading, to loaded...
if anyone could help me with this... Very Happy


Yes, when you do the assignment to "table", you copy (so to speak, that's not really what happens, but seen from the outside, it is) the string value of the class attribute, you don't copy a reference to the class name itself. Hence, assigning 'loaded' to the "table" variable will just change the value of that variable, not what you originally copied its value from.

So, you'd rather use the setAttribute method, but since that's not universally supported, use, as Straevaras suggests, the className property.

On a side note, the className property is a W3C standard, and (subjectively) should be preferred over setAttribute - only use set/getAttribute for element attributes which you can't access any other way - for most attributes, there's a property on the element, such as:

element.id
element.title
link.href
link.type
link.rel
anchor.href

etc. etc.
alalex
yeah thanks! Very Happy
now i used this:
Code:
var table = document.getElementById('table_container').className = 'loaded';

and it worked really well!
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.