javascript insert table row at index

Solutions on MaxInterview for javascript insert table row at index by the best coders in the world

showing results for - "javascript insert table row at index"
Alyssa
04 Aug 2016
1<table id="ThisTable">
2  <tr><th>Head1</th><th>Head2</th><th>Head2</th></tr>
3  <tr><td>Data1</td><td>Data1</td><td>Data1</td></tr>
4  <tr><td>Data2</td><td>Data2</td><td>Data2</td></tr>
5</table>
6<script>
7  var table = document.getElementById("ThisTable");
8  var row = table.insertRow(-1);
9  <!-- Filling the new created cell -->
10  var cell1 = row.insertCell(0);
11  var cell2 = row.insertCell(1);
12  var cell3 = row.insertCell(2);
13  cell1.innerHTML = "NEW CELL1";
14  cell2.innerHTML = "NEW CELL2";
15  cell3.innerHTML = "NEW CELL3";
16</script>
17<!-- The (-1) indexes the last position of the table -->
Dario
26 Mar 2016
1// Find a <table> element with id="myTable":
2var table = document.getElementById("myTable");
3
4// Create an empty <tr> element and add it to the 1st position of the table:
5var row = table.insertRow(0);
6
7// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
8var cell1 = row.insertCell(0);
9var cell2 = row.insertCell(1);
10
11// Add some text to the new cells:
12cell1.innerHTML = "NEW CELL1";
13cell2.innerHTML = "NEW CELL2";
similar questions
queries leading to this page
javascript insert table row at index