0

Just learning jQuery and cannot get my variable into src when I use append. It either doesn't work at all, or I just get the string representation of my variable name when I look in the console.

This is my offending code:

var $googleURL = "http://maps.googleapis.com/maps/api/streetview?size=600x300&location="+$googleStreet+","+$googleCity;

$($body).append('<img src='$googleURL'></img>');

I don't want to use attr because there is no img tag on the page, just a body tag. Where did I go astray?

ToddT
  • 3,084
  • 4
  • 39
  • 83
  • You use `+` to concat variables in jQuery. Also, there is no need using `$` as a variable-precursor in jQuery. Could be worth reading this: http://stackoverflow.com/questions/205853/why-would-a-javascript-variable-start-with-a-dollar-sign – junkfoodjunkie Dec 01 '16 at 22:31

2 Answers2

2

please try

  $($body).append("<img src='"+ $googleURL + "'></img>");
1

With Javascript, you can put variables inside strings using +

Like this: "string, " + variable + ", more string"

Try this code, it may work depending on what you're trying to accomplish.

var googleURL = 'http://maps.googleapis.com/maps/api/streetview?size=600x300&location='+googleStreet+','+googleCity;

$($body).append('<img src="' + googleURL + '"></img>');

arbybruce
  • 188
  • 1
  • 11