-4

I want to add border to my table using javascript so when I am using

var table=document.createElement("table").style.border="1px solid";

and try to append the row to this table like this

 table.appendChild(newRow);

above line is throwing exception as follows:

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

and if I try to execute the same code without giving border it executes properly please help me with this.

Afaq Ahmed Khan
  • 2,164
  • 2
  • 29
  • 39
  • Please click [edit](https://stackoverflow.com/posts/53189556/edit), then click the snippet editor `[<>]` and create a [mcve] – mplungjan Nov 07 '18 at 12:34
  • 2
    if you `console.log(table)`, you'll see it's not a Node element, but a string. You need to separate your line into two. – cmbuckley Nov 07 '18 at 12:34
  • 1
    Duplicate of many. For example https://stackoverflow.com/questions/27079598/uncaught-typeerror-failed-to-execute-appendchild-on-node-parameter-1-is-no – mplungjan Nov 07 '18 at 12:35
  • Please perform [a minimum of research](https://www.google.nl/search?q=Failed+to+execute+%27appendChild%27+on+%27Node%27+style+site:stackoverflow.com) – mplungjan Nov 07 '18 at 12:36

1 Answers1

3

You're assigning "1px solid" to the border property, and the result of evaluating that (also "1px solid") to table.

It is a string, so it is quite natural that it doesn't have an appendChild method.

If you want to access the table itself later on, then you need to store the table itself in a variable.

var table = document.createElement("table");
table.style.border = "1px solid";
table.appendChild(newRow);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • https://www.google.nl/search?q=Failed+to+execute+%27appendChild%27+on+%27Node%27+style+site:stackoverflow.com – mplungjan Nov 07 '18 at 12:36