0

I created a table on my document using: document.body.appendChild(table)

now every time I make a change on my table I want to run

document.body.removeChild(table)
document.body.appendChild(table)

so that the content of the table on my page is updated. This does however not work in the first place as I get

app.js:75 Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.

as an error message, and it also seems like a really clunky solution to the problem.

2
  • But first perform a minimum of research google.nl/search?q=dom+update+html+table Commented Oct 16, 2018 at 16:57
  • I know I tried but I'm fairly new so I dont always find the correct key terms :) Commented Oct 18, 2018 at 8:32

2 Answers 2

1

now every time I make a change on my table I want to run...so that the content of the table on my page is updated

There's no need to do that. Just modify the table. It's an object in the DOM; modifications to it are rendered by the browser. This is fundamental to how the DOM works.

var table = document.createElement("table");
var tbody = document.createElement("tbody");
table.appendChild(tbody);
document.body.appendChild(table);
var timer = setInterval(function() {
    // This modifies the existing table
    tbody.insertAdjacentHTML(
        "beforeend",
        "<tr><td>" + tbody.children.length + "</td></tr>"
    );
    if (tbody.children.length == 5) {
        clearInterval(timer);
    }
}, 500);

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

Comments

0

Your appended table is returned by appendChild. You can use it to modify in future.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.