TypeError: appendChild is not a function in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# TypeError: appendChild is not a function in JavaScript

The "TypeError: appendChild is not a function" occurs for multiple reasons:

  • calling the appendChild() method on a value that is not a DOM element.
  • placing the JS script tag above the code that declares the DOM elements.
  • misspelling appendChild - it's case-sensitive.

typeerror appendchild is not a function

Here is an example of how the error occurs.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div class="box">Box 1</div> <div class="box">Box 2</div> <div class="box">Box 3</div> <!-- โœ… Your JS script here โœ… --> <script src="index.js"></script> </body> </html>
The code for this article is available on GitHub

Notice that the JS script tag is placed at the bottom of the body tag.

Had we placed the JS script tag above the code that declares the DOM elements, they wouldn't be accessible in the index.js file.

Here is the code in index.js.

index.js
const boxes = document.getElementsByClassName('box'); console.log(boxes); // ๐Ÿ‘‰๏ธ [div.box, div.box, div.box] const child = document.createElement('div'); child.innerHTML = `<h1>Hello world</h1>`; // โ›”๏ธ TypeError: appendChild is not a function boxes.appendChild(child);

We called the Node.appendChild method on a NodeList and not on a DOM element, which caused the error.

# Only call appendChild on valid DOM elements

Make sure to only call the appendChild method on valid DOM elements and place the JS script tag at the bottom of the body, after the DOM elements have been declared.

index.js
const boxes = document.getElementsByClassName('box'); console.log(boxes); // ๐Ÿ‘‰๏ธ [div.box, div.box, div.box] const child = document.createElement('div'); child.innerHTML = `<h1>Hello world</h1>`; // โœ… Works boxes[0].appendChild(child);

only call appendchild on valid dom elements

The code for this article is available on GitHub

By accessing the element at index 0 of the NodeList we got a DOM element, on which we can safely call the appendChild method.

# Check if the DOM element exists before calling appendChild()

If the element you're calling the method on sometimes doesn't exist, conditionally check if the element is there before calling the appendChild method.

For example, a basic DOM element has a type of object, so we can check if the value is an object and contains the appendChild property before calling the method.

index.js
const box = null; if ( typeof box === 'object' && box !== null && 'appendChild' in box ) { const child = document.createElement('div'); child.innerHTML = `<h1>Hello world</h1>`; box.appendChild(child); }
The code for this article is available on GitHub

Our if condition uses the logical AND (&&) operator, so for the if block to run, all of the conditions have to be met.

We first check if the box variable stores a value with a type of object because DOM elements have a type of object.

Then we check if the variable is not equal to null. Unfortunately, if you check the type of null with console.log(typeof null), you will get an "object" value back, so we have to make sure the value is not null.

index.js
console.log(typeof null); // ๐Ÿ‘‰๏ธ object
The last thing we check for is that the object contains the appendChild property.

Then we know we can safely call the appendChild method on the object.

This approach is called duck-typing.

When using duck-typing, we simply check if the object implements specific properties or methods and if it does, we assume it's an object of the correct type.

# Conclusion

To solve the "TypeError: appendChild is not a function", make sure to only call the appendChild method on valid DOM elements and place the JS script tag at the bottom of the body, after the DOM elements have been declared.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev