1

So I have the following code:

function moveIt(var1,var2,var3){
    document.getElementById(var1).style.var2 = var3;
}

And then call it using

window.setTimeout("moveIt('square','backgroundColor','blue')",1500);

But it stays the same. Changin var2 to backgroundColor in the first code makes it work.

What can I do to solve it?

Thanks.

HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
dult.np
  • 67
  • 10
  • possible duplicate of [Dynamic object property name](http://stackoverflow.com/questions/4244896/dynamic-object-property-name) – Bergi Aug 29 '13 at 13:03
  • 2
    You need to use [bracket notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Member_Operators): `….style[var2]`. And don't pass strings to [`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/window.setTimeout), pass functions! – Bergi Aug 29 '13 at 13:04
  • 1
    Solved. Thanks. Still Learning! – dult.np Aug 29 '13 at 13:05
  • @user2075203:remove that solved from title..just accept the answer you think is correct.. – HIRA THAKUR Aug 29 '13 at 15:05

2 Answers2

3

Try this

 document.getElementById(var1).style[var2] = var3;

It's an applied version of this concept.

Community
  • 1
  • 1
Nick
  • 5,995
  • 12
  • 54
  • 78
2

try

window.setTimeout(function(){moveIt('square','backgroundColor','blue')},1500);

with

function moveIt(var1,var2,var3){
    document.getElementById(var1).style[var2] = var3;
}
Moazzam Khan
  • 3,130
  • 2
  • 20
  • 35