How do I put a variable in a string in JavaScript?
Example:
$(\'#contactForm\').animate({\"marginLeft\": \"+=680px\"}, \"slow\");
I wou
var size = "680px";
$('#contactForm').animate({"marginLeft": "+="+size}, "slow");
You can use the +
operator to concatenate your value in a variable to the "+="
string:
$('#contactForm').animate({"marginLeft": "+=" + size}, "slow");
var theWidth = "680px"
$('#contactForm').animate({marginLeft: "+=" + theWidth}, "slow");
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"