3

I need to choose a property to animate and then, do animation.

The code should be like a following:

var prop = "background-color";
switch( val )
{
   case 1: prop = "color";
   case 2: prop = "border-color";
   // ...
}
item.animate( {prop: "#00FF00"}, 1000 );

JavaScript complains about using "prop" variable.

When i just say

item.animate( {"color": "#00FF00"}, 1000 );

everything is fine.

I think, a constant is expected as object property declaration.

How can i determine it at runtime ?

2 Answers 2

5

These are equivalent:

// prop is a literal string here,
// not a variable
{prop: "#00FF00"}

and

{"prop": "#00FF00"}

you probably need to do something like this:

var obj = {};
obj[prop]="#0000ff";
item.animate( obj, 1000 );
Sign up to request clarification or add additional context in comments.

Comments

2

Yes, you're correct, JavaScript expects an identifier as the property name in an object literal. You'll have to create an object and assign the property using square bracket notation.

var opts = {};
opts[prop] = "#00FF00";
item.animate(opts, 1000);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.