0

Trying to get my dynamic table to have a button in last column. Have had no luck. Any help much appreciated.

var removeRow=document.createElement("BUTTON");

                //Add the data rows.
                for (var i = 1; i < data.length; i++) {
                    row = table.insertRow(-1);
                    for (var j = 0; j < 3; j++) {
                        var cell = row.insertCell(-1);
                        if (j==0) {
                        cell.innerHTML = data[i].userId}
                        if (j==1) {
                        cell.innerHTML = data[i].id}
                        if (j==2) {
                        cell.innerHTML = data[i].title}
                        if (j==3) {
                        cell.appendChild(removeRow)// Not working when replace data[i].field with button variable.
                    }
                }
2
  • What is cell? Without the html it's hard to tell, but possibly try: cell.parentElement.appendChild(removeRow) Commented Nov 19, 2018 at 22:19
  • That is strange, do it in one line, do a statment. cell.innerHTML="<td>"+data[i].userId ....+"<\td>". Commented Nov 19, 2018 at 22:22

1 Answer 1

3

In your loop, j never gets to 3 (it says j < 3 in your second for statement). If you change that to j < 4 or j <= 3 it should work.

Apart from that, you are only creating one BUTTON element, which you will be appending to all rows. Every time you append it to a row, it will be removed from the previous row it was on, so you'll still be left with just one button.

Sign up to request clarification or add additional context in comments.

1 Comment

I have corrected 3 to 4. Cell is derived for the row below var row = table.insertRow(-1); for (var i = 0; i < columnCount; i++) { var headerCell = document.createElement("TH"); headerCell.innerHTML = 'Header1'; row.appendChild(headerCell); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.