I want to add an index number to each row in a table automatically, like the one in the screenshot that I got from bootstrap's site here, I have tried multiple solutions but kept getting an index of -1 for all the rows, I would really appreciate the help, thank.
CodePudding user response:
Does this work for you?
// tbl is the table
var rows = tbl.rows; n = 1;
for (i=0; i<rows.length; i ) {
row = rows[i];
f = row.firstElementChild;
if (f) {
el = document.createElement(f.tagName);
el.innerHTML = (f.tagName == "TH") ? "#" : n;
row.insertBefore(el, f);
if (f.tagName != "TH") n ;
}
}