Reputation: 31
I asked this question because I was writing code and I focused on this problem. Practically I created a table dynamically within a loop and I would like to give each column a name like "clm" + i. where i is a variable that increases each time at the end of the cycle. Is it possible to do it?
per esempio
for(...)
<table>
<tr> <td id='clm'+i></td></tr>
</table>
i++;
Is the positioning of the quotation marks correct?
Upvotes: 2
Views: 50
Reputation: 492
You are pretty close to what I think you want to accomplish.
Basically, you want something along the lines of this:
var myRow = document.getElementById("myRow");
for (i = 0; i < 5; i++) {
myRow.innerHTML += '<td id="clm' + i + '">Cell</td>';
}
console.log(myRow.innerHTML); // You can also check the cells with inspect element
<table>
<tr id="myRow"></tr>
</table>
The example code here will find a row with the id myRow
and simply append new td
tags on the inside.
To note here is the '<td id="clm' + i + '">Cell<td>'
part. As you can see I close the quotes and concatenate i
in between to get the number into the id attribute.
Upvotes: 1