I have a javascript code for creating a row of a table:
function create_row() {
var table = document.getElementById("main_table");
var n = table.rows.length;
var m = table.rows[0].cells.length;
var row = table.insertRow(n);
for (i=2; i<m; i++) {
var cell = row.insertCell(i);
cell.innerHTML = "<input size=4>";
cell.addEventListener("change", function () {
console.log("Column of a cell: " + cell.cellIndex);
})
}
}
document.getElementById('create_row').onclick = create_row;
My intention is that when a user types something in the cell, I want to print that value. However, cellIndex
is always the number of columns in my table (or m
) (as I understand, because the cell is clicked after all cells are created, and thus i
reached m
).
How can I receive row, column, and the value of a cell when the content of it was changed?