0

How do I put a variable in a string in JavaScript?

Example:

$('#contactForm').animate({"marginLeft": "+=680px"}, "slow");

I would like to use in a variable instead of 680px, is there a way to do this?

4 Answers 4

3

As with most languages that don't have sigals, you can't perform interpolation, you have to concatenate multiple strings.

"foo" + "bar" + "baz"
"foo" + string_var + "baz"
"+=" + string_var + "px"
Sign up to request clarification or add additional context in comments.

Comments

1
var size = "680px";
$('#contactForm').animate({"marginLeft": "+="+size}, "slow");

Comments

0

You can use the + operator to concatenate your value in a variable to the "+=" string:

$('#contactForm').animate({"marginLeft": "+=" + size}, "slow");

Comments

0
var theWidth = "680px"
$('#contactForm').animate({marginLeft: "+=" + theWidth}, "slow");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.