element.setAttribute('style', 'attribute :value;') vs. element.attribute = 'value'

Viewed 66339

In javascript is there any difference between using

element.style.setAttribute('width', '150px');

and

element.style.width = '150px';

?

I have seen that keywords won't work with the first way (like this), but for non-keyword attributes is there a difference?

3 Answers

To set multiple CSS values, the fastest is to use

el.setAttribute('style','LIST-OF-STYLES');

To use the "recommended" style object, it has to be

el.style.ST1 = 'VA1';
el.style.ST2 = 'VA2';
...

There is also el.style.setAttribute, but this is even more verbose and should appeal only to Java developers.

Related