Forum Moderators: open

Message Too Old, No Replies

Dynamically setting the style

Not working in IE

         

adni18

12:16 am on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How can style attributes be dynamically added to dynamically added elements? I tried blah.setAttribute("style","background:blue") This works in Mozilla, but not IE.

adni18

12:22 am on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nevermind. I figured out that in IE, I should use

blah.style.setAttribute("background","blue")

Bernard Marx

10:06 am on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm still hazy on this, but...

I'm not sure about the "official" position of a

setAttribute
method for the style object - whether it actually exists (OK, it works in IE; I reckon it won't work in others).

(If you asked me) I'd use either:

1. W3C DOM method..

blah.style.setPropertyValue("background-color","blue")

2. The "traditional"..

blah.style.backgroundColor = "blue";

#1 promises to be progressive, but it's not supported in IE5 (5.5?)

Rambo Tribble

2:02 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, my resources suggest that setAttribute() is the W3C preferred method (as opposed to the technique in your second example, Mr. Marx) for altering attributes in a DOM 1 context. setProperty() is a DOM 2 method, which Flanagan indicates is not available in IE Win.

Rambo Tribble

3:39 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oopsie! That attribution should be to Goodman, not Flanagan.

Oh, you know, one of those ~an people. You know the ones.