How to append text to all values of javascript Array

You need to make sure you close your image tag. Another thing that may cause the problem is that i is undefined. Does your browser give an error message?

var str = "23423,1616,3461743,1345";
var PhotoArray = str.split(",");
for ( var i = 0; i < PhotoArray.length; i++ ) {
    PhotoArray[i] = "<img src=\"" + PhotoArray[i] + "\"></img>";
}
str = PhotoArray.join("");

Some terrible answers here. Try:

"1,2,3,4".split(",").map(function(a) { return "<foo>" + a + "</foo>"; }).join("");

Or with slightly more modern Javascript:

"1,2,3,4".split(",").map(a => `<foo>${a}</foo>`).join("");

Also please be aware of HTML injection.

Tags:

Javascript