Here is my HTML code:
<input type='hidden' id='hiddenRowId' value='0' />
<a href='#' onclick='addItem(); return false' class='buttonAdd'>Add Item</a>
<table id='tb1'>
<tr>
<th>No</th>
<th>Seq</th>
<th>Item Name</th>
<th>Compulsory</th>
<th>Fixed Qty</th>
<th>Qty</th>
<th>Unit Price</th>
<th></th>
</tr>
</table>
and this is the javascript:
function addItem() {
hiddenRowId = document.getElementById('hiddenRowId');
let rowid = parseInt(hiddenRowId.value);
if (isNaN(rowid)) {
rowid = 0;
}
rowid--;
hiddenRowId.value = rowid;
let tb1 = document.getElementById('tb1');
let tr = tb1.insertRow(-1);
tr.id = "tr" + rowid;
let cell1 = tr.insertCell(-1);
let cell2 = tr.insertCell(-1);
let cell3 = tr.insertCell(-1);
let cell4 = tr.insertCell(-1);
let cell5 = tr.insertCell(-1);
let cell6 = tr.insertCell(-1);
let cell7 = tr.insertCell(-1);
let cell8 = tr.insertCell(-1);
cell2.innerHtml = `<input type='text' name='txt_${rowid}_seq' value='' style='width: 40px;' />`;
cell3.innerHtml = `<input type='text' name='txt_${rowid}_name' value='' style='width: 300px;' />`;
cell4.innerHtml = `<input type='checkbox' name='txt_${rowid}_compulsory' />`;
cell5.innerHtml = `<input type='checkbox' name='txt_${rowid}_fixqty' />`;
cell6.innerHtml = `<input type='text' name='txt_${rowid}_qty' value='1' style='width 40px;' />`;
cell7.innerHtml = `<input type='text' name='txt_${rowid}_unitprice' value='' style='width: 80px;' />`;
cell8.innerHtml = `<a href='#' onclick='remove(${rowid}); return false;' class='buttonmain'>Delete</a>`;
}
This the javascript is somehow not working. The row was added, but there was no cells added. Do you know what is the missing steps? The link button "Add Item" is not working.
https://jsfiddle.net/L09up1rj/
Is there any more appropriate way to do this?