How to solve: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'

2

I have a problem with an application using ES6. I am creating a table that receives values from a form, but every time I click the button to send the information to the table, it gives the following error.

  

Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.

I wonder if anyone can help me.

Code that gives the error:

 var campos = [
    document.querySelector('#data'),
    document.querySelector('#quantidade'),
    document.querySelector('#valor')
];

console.log(campos);

var tbody = document.querySelector('table tbody');

document.querySelector('.form').addEventListener('submit', function (event) {

    event.preventDefault();

    var tr = document.createElement('tr');

   campos.forEach(function (campo) {
       var td = document.createElement('td');
       td.textContent = campo.value;
       td.append('td');
       td.appendChild(td);
       // td.appendChild('td');
       // td.appendChild(td);
   });

   var tdVolume = document.createElement('td');
   tdVolume.textContent = campos[1].value * campos[2].value;

   tr.appendChild(tdVolume);

   tbody.appendChild(tr);

   campos[0].value = '';
   campos[1].value = 1;
   campos[2].value = 0;

   campos[0].focus();

});
    
asked by anonymous 29.12.2017 / 19:29

1 answer

2

I believe you are trying to insert the td into itself. In the code below I changed to insert the td in tr. See if it solves your problem.

campos.forEach(function (campo) {
   var td = document.createElement('td');
   td.textContent = campo.value;
   td.append('td');
   tr.appendChild(td);
});
    
29.12.2017 / 19:38