Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable in javascript statement

Tags:

javascript

css

How does one add a variable string in this javascript statement? where name may correspond to any valid string , say WebkitTransform or Moztransform,etc

document.getElementById('test').style.VARIABLE_NAME  =  'rotate(15deg)';

My code doesn't seem to work when i set the VARIABLE_NAME to WebkitTransform, but it works fine if I use WebkitTransform directly, as in without naming it via a variable. Thanks in advance :)

like image 409
Neha Avatar asked Aug 08 '11 21:08

Neha


1 Answers

There are two ways to access members of a Javascript object.

Dot notation, which uses an identifier to access the member:

obj.member;

Bracket notation, which uses a string to access the member:

obj['member']

The latter uses a string to locate the member and you can just as easily use any expression. The value of the expression will be converted to a string so these are equivalent:

obj[{}]
obj['[object Object]']

If your expression is already a string it will be used as is, and in your case your variable holds a string so you can just do:

document.getElementById('test').style[VARIABLE_NAME]  =  'rotate(15deg)';
like image 197
Paul Avatar answered Sep 26 '22 02:09

Paul