I encountered an issue with my code, where I successfully created a <th>
element initially, but faced problems when trying to create it repeatedly.
function createTH(){
var noOfRow = document.getElementById("addItemTable").rows.length;
var temp = document.getElementById("addItemTable");
var table = temp.getElementsByTagName('tbody')[0];
var row = table.insertRow(-1);
var cell2 = row.insertCell(0);
var cell3 = row.insertCell(1);
cell2.innerHTML="this is inside table div";
cell2.style="border: dashed;"
cell3.innerHTML="this is inside another div";
cell3.style="border: dashed;"
var thContent = '<th class="col2">' + '<br>' + 'test' + '   ' + '*' + '' + '</th>'
var mainTable = document.getElementById("addItemTable");
$('#addItemTable>tbody>tr').prepend(thContent);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="m-content" id="elementDiv">
<table id="addItemTable">
<tbody>
</tbody>
</table>
</div>
<input type="button" onclick="createTH()" value="Click to create <th> again and again"/>
Upon clicking the button for the second time, the output of my code becomes chaotic.
I aim to achieve the following output pattern:
<tr>
...
<th><td><td>//I only want to repeat this single line on every click of the button.
<th><td><td>
<th><td><td>//I'm looking to create this kind of repetition on the click of the button.
...
</tr>
For further details, you can refer to: Dynamic Creation of th. This question serves as a follow-up to the previous discussion thread. Creating a new thread was necessary due to the complexity and length constraints of the original question.
Your assistance in resolving this issue would be highly appreciated.