Reputation: 1948
here my code which is not working properly. (it is working if on line 4 I write $("img").attr("src", image_src_1); )
I guess I have to "variablize" line 4.
<script>
var image_src_1 = "image.jpg";
var x = 1;
var new_source_for_image = "image_src_" + x; // I WANT IT TO BE image_src_1
$("img").attr("src", new_source_for_image); // (line 4)
</script>
<body>
<img src="">
</body>
Upvotes: 1
Views: 6825
Reputation: 603
Take a look at eval http://www.w3schools.com/jsref/jsref_eval.asp. But, be careful to make sure you trust the data you run through eval. User data can be executed.
Upvotes: 1
Reputation: 82231
that is because new_source_for_image
represents name of variable and not variable itself. You need to use .eval()
for evaluating the value out of it:
$("img").attr("src", eval(new_source_for_image));
Upvotes: 2