Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
167
rated 0 times [  172] [ 5]  / answers: 1 / hits: 69637  / 15 Years ago, wed, december 9, 2009, 12:00:00

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?


More From » html

 Answers
30

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.


[#98110] Saturday, December 5, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
albert

Total Points: 652
Total Questions: 105
Total Answers: 108

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
;