Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string concatenation using javascript [closed]

I'm trying to build a HTML string in the following way:

htmlString = '<html>';
var headerString = "image1";
var sImage = "Android_images/"+headerString+".png";
htmlString += '<img src='+sImage+' />';
htmlString = '</html>';

I need to dynamically append a image string, but it shows:

<img src=Android_images/dfdfd.png />
like image 790
vishnu Avatar asked Apr 30 '13 05:04

vishnu


People also ask

Can you concatenate strings in JavaScript?

The concat() method joins two or more strings. The concat() method does not change the existing strings. The concat() method returns a new string.

What is the best way to concatenate strings in JavaScript?

The best and fastest way to concatenate strings in JavaScript is to use the + operator. You can also use the concat() method.

How do you concatenate variables in JavaScript?

In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect. let myPet = 'seahorse'; console.

Can we concatenate string and number in JavaScript?

In javascript the "+" operator is used to add numbers or to concatenate strings. if one of the operands is a string "+" concatenates, and if it is only numbers it adds them.


1 Answers

You're re-setting the variable on this last line:

htmlString = '</html>';

Add a + and it'll work:

var htmlString = '<html>';
var headerString = "image1";
var sImage = "Android_images/" + headerString + ".png";
htmlString += '<img src="' + sImage + '" />';
htmlString += '</html>';

Also, why are there <html> tags here?

like image 102
Blender Avatar answered Sep 18 '22 00:09

Blender