What is difference betweet betweet element.style['property']='value'
and element.style.property='value'
for manipulate style in javascript?
Asked
Active
Viewed 39 times
0

user2799274
- 133
- 1
- 1
- 9
-
possible duplicate of [JavaScript property access: dot notation vs. brackets?](http://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets) – Josh Lee Sep 20 '13 at 17:38
2 Answers
1
There is no difference it's just different syntax
In JavaScript, objects are also associative arrays (or hashes). That is, the property
theStatus.Home
can also be read or written by calling
theStatus['Home']

Alex Art.
- 8,711
- 3
- 29
- 47
0
There is a difference in this case, but I believe it's non-standard and browser-dependent: in Chrome, bracket notation allows you to reference style properties using their actual CSS name, instead of the camelCased version. In Firefox, it doesn't work. I haven't tested other browsers.
You can try it with Chrome on this very page, from the console:
document.body.style['background-color'] = 'red'

bfavaretto
- 71,580
- 16
- 111
- 150
-
No it's the same, like z-index the name for manipulate element with javascript is a little different :) – user2799274 Sep 20 '13 at 18:15
-
I know, what I'm saying is that Chrome allows, `el.style.zIndex`, `el.style['zIndex']` and (which is unusual) `el.style['z-index']`. – bfavaretto Sep 20 '13 at 18:17
-
I understand, just have to use the right names and there are no problems ;) – user2799274 Sep 20 '13 at 19:40