element.setAttibute() method how to make it work in I.E?

1.8k views Asked by At

I am setting the style attribute of a text via js element.setAttribute() method with name=style and value="my modifications to the style of text"

it is working well in browsers other than IE ..

In order to make it possible ,what should i do ?

for your information i m modifying these attributes -- text-align,text-decoration,font-style,font-weight,font-size....

I will be happy if someone guides me thank you..

2

There are 2 answers

0
Shadow Wizard Love Zelda On BEST ANSWER

Another way for IE which "preserve" the ordinary syntax of CSS is cssText property:

element.style.cssText = "text-align: center; text-decoration: underline; font-size: 120%;";

Official documentation: http://msdn.microsoft.com/en-us/library/ms533698(v=vs.85).aspx

0
Quentin On

Just avoid setAttribute. It does nothing that can't be done though other methods.

element.style.textAlign = 'left';

for example.

That said, you are almost certainly better off predefining your styles and then causing them to be applied with:

element.className += 'someClass';

… or a library that implements addClass and removeClass methods.