views:

151

answers:

1

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?

+2  A: 

Both are perfectly valid. Can you give some examples which doesn't work in second way? Are you aware that attribute names needs to be camelcased first? E.g. element.style.marginTop instead of incorrectly element.style.margin-top. The hyphen is namely an invalid identifier in Javascript.

BalusC
That's what I thought. Sorry, I was confused about the keywords not working part, will update to fix. The thing is that the first way won't work in Safari, so I wanted to make sure the second way was fine for IE and Safari.
rosscj2533