Home > Software design >  how to get Different name for each td?
how to get Different name for each td?

Time:12-08

I need some help, what should I do so that all the rows have each one a different id for example:introdu1,introdu2,introdu3

var perrow = 1; 
array.forEach((value, i) => {

  myTable  = `<td id="ore">${value}</td>`;
  myTable  = `<td id="introdu" > </td>`;
  
  
  var next = i   1;
  if (next%perrow==0 && next!=array.length) { myTable  = "</tr><tr>"; }
});

CodePudding user response:

It is very easy you can use ${variable} like you do in ${value}

let array = ['r','4','6'];  
let myTable = "";
var perrow = 1; 
    array.forEach((value, i) => {
    var next = i   1;
    
      myTable  = `<td id="ore${next}">${value}</td>`;
      myTable  = `<td id="introdu${next}" > </td>`;
      
      if (next%perrow==0 && next!=array.length) { myTable  = "</tr><tr>"; }
    });
    console.log(myTable);
td {
background : red;
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Use a dynamically generated ID:

var perrow = 1;
var id = 1;
array.forEach((value, i) => {

  myTable  = `<td id="ore${id}">${value}</td>`;
  myTable  = `<td id="introdu${id  }" > </td>`;

  var next = i   1;
  if (next % perrow == 0 && next != array.length) {
    myTable  = "</tr><tr>";
  }
});
  • Related