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?

1
  • You use + to concat variables in jQuery. Also, there is no need using $ as a variable-precursor in jQuery. Could be worth reading this: stackoverflow.com/questions/205853/… Commented Dec 1, 2016 at 22:31

2 Answers 2

2

please try

  $($body).append("<img src='"+ $googleURL + "'></img>");
Sign up to request clarification or add additional context in comments.

Comments

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>');

Comments

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.