-1

I have a simple problem. I am getting this carInfo data from a JSON file but can't get the img source to properly part the variable $imgsrc instead of parsing it as a string called "$imgsrc". Then I am appending this to my HTML code.

var $imgsrc = "images/vehicles/" + carInfo[i].smallImage;
var $container = .... + '<img src = '$imgsrc'>' + ....;

$('body').append($container);

EDIT:

+ ... '<img src = "images/vehicles/ + '$imgsrc'">' + ...

This gives me a error due to the opening single quote of $imgsrc is actually closing the single quote in the beginning of the tag

BaBazinga
  • 159
  • 1
  • 13
  • What does "part the variable $imgsrc " mean in your question? – jfriend00 Jun 03 '15 at 23:29
  • If you want `''` to be interpreted as something meaningful, you should probably write something like `''`. – Sebastian Simon Jun 03 '15 at 23:34
  • your question is related with the following link http://stackoverflow.com/questions/8013792/how-to-create-a-new-img-tag-with-jquery-with-the-src-and-id-from-a-javascript-o –  Jun 03 '15 at 23:57

2 Answers2

3

shouldnt you just do this:

var $container = .... + '<img src = '+ $imgsrc +'>' + ....;
reyaner
  • 2,799
  • 1
  • 12
  • 17
0

You need to concatenate the var to the string

var $container = .... + '<img src="' + $imgsrc + '">' + ....;
Juank
  • 6,096
  • 1
  • 28
  • 28