user2374907
user2374907

Reputation: 55

Javascript append tr to table

Hi I have a function where I need to append the result to the end of the table but it gives me an error "that the Argument 1 of Node.appendChild is not an object."

Here is my code

window.onload = function() {
    var mainTable = document.getElementById('mainTable');
    var types = document.getElementsByClassName('typeCell');
    var rentType = "Rent";

    for (var i = 0; i<types.length; i++) {
        if(types[i].innerHTML.trim() == rentType) {

            mainTable.appendChild(types[i].parentNode.outerHTML.trim());
        }
    }
}

Upvotes: 0

Views: 82

Answers (2)

user2224705
user2224705

Reputation: 1

types[i].parentNode.outerHTML.trim() just can return a String

but the argument of "Node.appendChild()" must be a Object

Upvotes: 0

Armand Grillet
Armand Grillet

Reputation: 3399

With types[i].parentNode.outerHTML.trim() you are giving a string to the functon mainTable.appendChild and, as the error said, you have to give an HTML element.

Modify your code to have mainTable.appendChild(types[i].parentNode.clone()); in your if condition (I'm not sure about this solution because I cannot test it, but it should be something like that).

Upvotes: 2

Related Questions