Last updated: Mar 7, 2024
Reading time·4 min
The error "Failed to execute 'appendChild' on 'Node': parameter 1 is not of
type 'Node'" occurs when you call the appendChild()
method with a value that
is not a DOM Node.
To solve the error, make sure to pass a node to the appendChild()
method.
Here is the complete error message.
Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
Here is an example of how the error occurs.
This is the code for the index.html
file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="box">bobbyhadz.com</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const box = document.getElementById('box'); // ⛔️ Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'. box.appendChild('<p>google.com</p>');
The issue in the code sample is that we called the appendChild() method with a string instead of a DOM Node.
To solve the error, make sure to call the appendChild()
method with a DOM node
instead.
const box = document.getElementById('box'); const paragraph = document.createElement('p'); paragraph.innerHTML = `google.<b>com</b>`; box.appendChild(paragraph);
We used the document.createElement() method to create an element and set the element's innerHTML.
The document.createElement
method takes the type of element you want to create
(e.g. div
, span
or p
).
The last step is to pass the DOM node to the appendChild()
method.
The appendChild()
method adds a node to the end of the list of children of the
element on which it was called.
appendChild()
method moves the element from its current position to the new position.The appendChild()
method takes a node (a DOM element) as a parameter.
If you try to call the method with a string or any other value, the error is raised.
You can use the document.createElement()
method to create a DOM element and
pass the result to the appendChild()
method.
The error commonly occurs when you pass an array of DOM elements to the
appendChild()
method.
Here is an example.
const box = document.getElementById('box'); const paragraph1 = document.createElement('p'); paragraph1.innerHTML = `google.<b>com</b>`; const paragraph2 = document.createElement('p'); paragraph2.innerHTML = `example.<b>com</b>`; const arrayOfElements = [paragraph1, paragraph2]; // ⛔️ Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'. box.appendChild(arrayOfElements);
p
elements.appendChild()
method.The appendChild()
method cannot be called with an array, so you have 2 options
in this case:
0
to get the first element.appendChild
method with each element.appendChild()
method with the first element from an arrayThe following code sample calls the appendChild()
method with the first
element from the array.
const box = document.getElementById('box'); const paragraph1 = document.createElement('p'); paragraph1.innerHTML = `google.<b>com</b>`; const paragraph2 = document.createElement('p'); paragraph2.innerHTML = `example.<b>com</b>`; const arrayOfElements = [paragraph1, paragraph2]; box.appendChild(arrayOfElements[0]);
We accessed the array at index 0
to get the first array element.
0
and the last has an index of array.length - 1
.appendChild()
method with each element from an arrayAnd here is an example that calls the appendChild()
method with each element
from an array.
const box = document.getElementById('box'); const paragraph1 = document.createElement('p'); paragraph1.innerHTML = `google.<b>com</b>`; const paragraph2 = document.createElement('p'); paragraph2.innerHTML = `example.<b>com</b>`; const arrayOfElements = [paragraph1, paragraph2]; for (const element of arrayOfElements) { box.appendChild(element); }
We used a for...of
loop to iterate over the array and called the
appendChild()
method with each element from the array.
insertAdjacentHTML
method instead of appendChild
An alternative approach is to use the
insertAdjacentHTML method instead
of appendChild
.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="box">bobbyhadz.com</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const box = document.getElementById('box'); box.insertAdjacentHTML('beforeend', '<p>google.<b>com</b></p>');
The insertAdjacentHTML()
method takes the following 2 parameters:
position
- the position relative to the element where the HTML should be
inserted. Can be one of the following:
beforebegin
- before the element itself.afterbegin
- just inside the element, before its first child.beforeend
- just inside the element, after its last child.afterend
- after the element itself.text
- the string to be parsed as HTML and inserted into the DOM.
We set the position
argument to beforeend
to insert the HTML string right
before the end of the div
element.
Notice that the insertAdjacentHTML()
can be passed an HTML string as its
second argument.
Passing an HTML string is not possible when using the appendChild()
method.
const box = document.getElementById('box'); box.insertAdjacentHTML('beforeend', '<p>google.<b>com</b></p>');
It should be noted that you shouldn't append HTML that is from user-generated input without escaping it.
This could leave your code susceptible to cross-site scripting attacks.
I've also written articles on: