Forum Moderators: not2easy

Message Too Old, No Replies

applying styles to elements with ids

via js

         

natty

12:41 pm on Sep 14, 2004 (gmt 0)

10+ Year Member



hi all,

this maybe should be in the javascript forum, but here goes.

i have a table, cells have ids, and will be updated dynamically with JS. question is , can i in fact apply a className to these table cells, (which in fact are spans with ids not tds) via javascript on the fly?

css will allow a classname and an id, i presume, and then the classname would take precedence over the id style in the cascade?

as i am wanting to change to color of the text depending on this that and the other...

i hope that explanation makes some sense.

tia
nat

DrDoc

2:55 pm on Sep 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



IDs always take precedence over classes. But, if it's just a matter of changing the color, all you have to do is simply not assign the color in the ID declaration :)

That leaves us with two options for how to change the styling:

document.getElementById('foo').className = 'bar';

OR

document.getElementById('foo').style.color = 'red';

Personally I would go with the latter...