I am trying to add a button for each row of the dynamically generated HTML table using JavaScript. Here is my code:
// helper function
function addCell(tr, text) {
var td = tr.insertCell();
td.innerHTML = text;
return td;
}
// insert data
list.forEach(function (item) {
var row = table.insertRow();
var button = document.createElement('button');
var bText = document.createTextNode('View');
button.appendChild(bText);
addCell(row, item.itemName);
addCell(row, '$ ' + item.total.toFixed(2));
addCell(row, button);
});
I managed to print out all other fields except the button. The column for the button just simply printed out this:
[object HTMLButtonElement]
Just wondering if there is anyway to add a image button to the dynamically generated table? Upon selecting the button, I wanted to get the value of the row and pass as parameter to another function as well.