2

I am attempting to give each row that is dynamically added a unique ID. Basically by adding to the number each time the user clicks the add button. It is adding an ID, but not correctly, it is showing up as "undefined" in the dev tools.

var counter = 0;
function appendRow(id, style) {
  var table = document.getElementById(id); // table reference
  length = table.length,
  row = table.insertRow(table.rows.length, 'id');      // append table row
  row.setAttribute('id', style);
  row.setAttribute('idName', style);
  var i;
  // insert table cells to the new row
  for (i = 0; i < table.rows[0].cells.length; i++) {
      createCell(row.insertCell(i), i, 'cust' + counter);
      counter++
  }
}

function createCell(cell, text, style) {
  var div = document.createElement('div'), // create DIV element
      txt = document.createTextNode('_'); // create text node
  div.appendChild(txt);               // append text node to the DIV
  div.setAttribute('id', style);        // set DIV class attribute
  div.setAttribute('idName', style);    // set DIV class attribute for IE (?!)
  cell.appendChild(div);                   // append DIV to the table cell
}
table { 
  text-align: center;
}
td { 
  width: 100px;
}


tr:nth-child(even) {
  background-color: #fff;
}
tr:nth-child(odd) {
  background-color: #eee;
}
<button id="addCust" class="addSort" onclick="appendRow('custList')">add customer</button>
<div class="custScroll">
        <table id="custListTop" contenteditable="false">
          <tr>
            <td style="border-top-left-radius: 5px;">Customers</td>
            <td style="border-top-right-radius: 5px;">Main Location</td>
          </tr>
        </table>
        <table id="custList" contenteditable="true">
          <tr>
            <td>Someone</td>
            <td>Somewhere</td>
          </tr>
        </table>
      </div>
user3613129
  • 411
  • 4
  • 13
hannacreed
  • 639
  • 3
  • 15
  • 34

2 Answers2

0

The reason why the new elements are showing up as "undefined" is because the style argument of appendRow has not been provided.

To get the functionality that you're going for you have to remove style from the appendRow arguments and replace the references to style inside appendRow with 'cust' + counter.

-1

Your style value is null here please check style value I have also added fiddle

Please check this code, When user is clicking on button the style value is undefined.

<button id="addCust" class="addSort" ***onclick="appendRow('custList')"***>add customer</button>

Appendrow function requires two parameters and you are just passing one.

var counter = 0;
$('#addCust').click(function() {
  var table = document.getElementById('custListTop'); // table reference
  length = table.length,
  row = table.insertRow(table.rows.length, 'id'); // append table row
  row.setAttribute('id', counter);
  row.setAttribute('idName', counter);
  var i;
  // insert table cells to the new row
  for (i = 0; i < table.rows[0].cells.length; i++) {
    createCell(row.insertCell(i), i, 'cust' + counter);
    counter++
  }
});


function createCell(cell, text, style) {
  var div = document.createElement('div'), // create DIV element
    txt = document.createTextNode('_'); // create text node
  div.appendChild(txt); // append text node to the DIV
  div.setAttribute('id', style); // set DIV class attribute
  div.setAttribute('idName', style); // set DIV class attribute for IE (?!)
  cell.appendChild(div); // append DIV to the table cell
}
table {
  text-align: center;
}

td {
  width: 100px;
}

tr:nth-child(even) {
  background-color: #fff;
}

tr:nth-child(odd) {
  background-color: #eee;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="addCust" class="addSort">add customer</button>
<div class="custScroll">
  <table id="custListTop" contenteditable="false">
    <tr>
      <td style="border-top-left-radius: 5px;">Customers</td>
      <td style="border-top-right-radius: 5px;">Main Location</td>
    </tr>
  </table>
  <table id="custList" contenteditable="true">
    <tr>
      <td>Someone</td>
      <td>Somewhere</td>
    </tr>
  </table>
</div>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
nikunjM
  • 560
  • 1
  • 7
  • 21
  • Please don't post code to 3rd party sites as those links can die over time. Just insert a code snippet, right here into your answer. – Scott Marcus Nov 13 '17 at 19:44