user1760904
user1760904

Reputation: 13

Javascript variables within attributes

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.

Upvotes: 1

Views: 97

Answers (1)

Amber
Amber

Reputation: 527488

Change...

"SomeFunction((x))"

to...

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

JavaScript doesn't support string interpolation of variables.

Upvotes: 2

Related Questions