You're overriding the content of the tr
when setting some innerHTML
to it. This wipes out the first created td
, and if there's not existing row.cells[0]
, you can't create row.cells[1]
. This causes the invalid index error.
You can fix your problem by setting the innerHTML
of the newly created cell instead:
for (var i = 0; i < cells; ++i) {
var cell = row.insertCell(i);
cell.className = "success"; // or row.className?
cell.innerHTML = "OMG";
}