0

I have HTML & javascript code where you can click a button to add a row to an existing table. I want to make sure that each row's dropdowns have the correct IDs in ascending order. The middle rows can get deleted, and I need to adjust the IDs.

function addRow(){
    var table = document.getElementById('myTable');

    //Get the number of rows in the table, and use (+1) value for the IDs later
    var rowCount = table.tBodies[0].rows.length;

    var tbodyRef = table.getElementsByTagName('tbody')[0]; 
    // Insert a row at the end of table
    var newRow = tbodyRef.insertRow();
    // Insert a cell at the end of the row
    var cell0 = newRow.insertCell(-1);
    cell0.innerHTML = '<select id="firstDropdown' + (rowCount + 1) + '"></select>';
    var cell1 = newRow.insertCell(-1);
    cell1.innerHTML = '<select id="secondDropdown' + (rowCount + 1) + '"></select>';
    var cell2 = newRow.insertCell(-1);
    cell2.innerHTML = '<button onclick=deleteRow()>Delete</button>';

    /*Here, I want to get the list of ID's of the firstDropdown of 
    each row (for debugging purposes). But this is not printing anything.
    Why is this code below not printing the first dropdown's ids of each row? */
    console.log("------------------------");
    for(var i=0; i<table.tBodies[0].rows.length; i++){
        console.log(table.tBodies[0].rows[i].cells[0].id);
    }
}

function deleteRow(){
    var td = event.target.parentNode;
    var tr = td.parentNode;
    tr.parentNode.removeChild(tr);
}

My HTML code is below:

<button onclick=addRow()>Add row</button>
<br>
<table id="myTable">
<thead>
    <th>1st dropdown</th>
    <th>2nd dropdown</th>
    <th>Remove</th>
</thead>
<tbody>
    <!--Rows will be added/deleted dynamically-->
</tbody>
</table>

I will need to update dropdowns' IDs later when rows are deleted. I was hoping to do it the same way: table.tBodies[0].rows[i].cells[0].id = "firstDropdown" + (i+1); but I am not sure if this is even going to work now.

J.Doe
  • 147
  • 1
  • 12
  • I'm curious why you would want to reissue ID's. Seems like that is adding complexity that might not need to be – Kinglish May 06 '21 at 19:27
  • I was trying some stuff with your code and my best guess is that you can't use tBodies when using thead and tbody. – Jonas May 06 '21 at 19:29
  • If not necessary, I will still need to get the values of 'option' selected in each dropdown. Both dropdowns will have more options, which I didn't include in the above code. How could I retrieve them? 'table.tBodies[0].rows[i].cells[0].value' returns undefined. – J.Doe May 06 '21 at 19:35
  • @J.Doe `.cells[0]` is a `` (it’s one of the `cells`, as the property name says), not a `` and ` – Sebastian Simon May 06 '21 at 19:49
  • Inline event handlers like `onclick` are [not recommended](/q/11737873/4642212). They are an [obsolete, hard-to-maintain and unintuitive](/a/43459991/4642212) way of registering events. Always [use `addEventListener`](//developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. – Sebastian Simon May 06 '21 at 19:52
  • Use [event delegation](//developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_delegation) instead of assigning multiple events — it’s more maintainable, and applies to dynamically added elements. E.g., use an [event argument](//developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#The_event_listener_callback)’s [`target`](//developer.mozilla.org/en-US/docs/Web/API/Event/target). See [the tag info](/tags/event-delegation/info) and [What is DOM Event delegation?](/q/1687296/4642212). – Sebastian Simon May 06 '21 at 19:53

0 Answers0