1

With php it's easy to do something like this

<?
$x = "Joe";
echo "My name is $x";

?>

But I'm having trouble doing something similar with javascript

var div = document.createElement("DIV");
    x="SomeValue";
    div.setAttribute("id", (x));
    div.setAttribute("onMouseDown", "SomeFunction((x))");

where obviously I want x to be "SomeValue", but every time I look at the output, it just says x instead of the value.

1
  • 5
    Don't set the onmousedown attribute, instead use div.onmousedown = function... or div.addEventListener('mousedown', function...). Commented Oct 20, 2012 at 3:37

1 Answer 1

2

Change...

"SomeFunction((x))"

to...

"SomeFunction((" + x + "))"

JavaScript doesn't support string interpolation of variables.

Sign up to request clarification or add additional context in comments.

1 Comment

@Amber yikes, I'm sorry about that. I meant "her" :p

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.