10

I'm creating a table dynamic table with Javascript:

var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);

I want to fill the cell5 with some innerHTML with a Javascript variable inside

var add = aux[i].split("#");
cell5.innerHTML = "<img src='MatrixLogos/add[3]' width='100' height='25'/>";

but this give add[3] in the html instead of the value inside add[3].

My question is how to escape the variable inside the innerHTML code, so it shows me is value and not the declaration.

1
  • There's currently no syntax for string interpolation. You can create a method that defines such behavior for strings, or you can simply concatenate the values in. Commented May 9, 2013 at 17:29

2 Answers 2

21

Use "+".

var add = aux[i].split("#");
cell5.innerHTML = "<img src='MatrixLogos/"+add[3]+"' width='100' height='25'/>";
Sign up to request clarification or add additional context in comments.

1 Comment

What if the value is saying undefined but after logging the variable in console we can see the result
2

Use String concatenation like

var add = aux[i].split("#");
var string = "This is to " + add[3] + " test with value";

Where as in your case, statement will become:

cell5.innerHTML = "<img src='MatrixLogos/"+add[3]+"' width='100' height='25'/>";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.