1

So I have this code and would like to know how I could go ahead to put my javascript var into this string. I cannot seem to make a working code for myself.

For the image source i want picture.value to be in there. I have tried different solutions but have not managed to work it out myself. All help is greatly appreciated

 var text = "<img src="">

I have currently been trying var text = "<img src=" + picture.value +"> but that doesn't seem to work for me.

2
  • 1
    What's the PHP? Maybe var text = '<img src="' + picture.value + '">'; Commented Mar 26, 2017 at 23:02
  • 1
    var text = '<img src=" ' + picture.value + ' " />" '; Commented Mar 26, 2017 at 23:04

3 Answers 3

3

You cannot use " & ' this together unless you escape it with a \.

 var text = '<img src="' + picture.value + '">'

OR

 var text = "<img src=\"" + "Hi" + "\">"
Sign up to request clarification or add additional context in comments.

Comments

3

Try using ES6 new feature called Template literals (using quote). It is more cleaner and simpler.

var picture = {'value':'test-src-location'};
var text = `<img src="${ picture.value }">`;
console.log(text);

Comments

0

Assuming your picture.value is not empty.

var text = '<img src=' + picture.value + '>';

Example: https://jsfiddle.net/no293etL/

1 Comment

This "kind of" works, but usually it's recommended to quote the attribute values.

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.