avatarRaja Tamil

Summary

The web content provides a comprehensive guide on using the appendChild() method in JavaScript to add one or more HTML elements to a DOM element, including examples of adding single and multiple elements, as well as nested elements.

Abstract

The article titled "Add Element To Div Using appendChild() JavaScript" on the undefined website is a tutorial that teaches web developers how to dynamically add HTML elements to a webpage using JavaScript. It explains the appendChild() method, which is used to append a single child element to a specified parent element. The tutorial includes a simple HTML markup example to illustrate the concept and provides JavaScript code snippets that demonstrate how to create new elements, set their properties, and append them to an existing DOM element. It also compares appendChild() to the append() method, highlighting their differences and use cases. The article further elaborates on how to add multiple child elements as siblings by calling appendChild() multiple times and how to add nested child elements one inside the other by chaining appendChild() method calls. Interactive CodePen examples are embedded for readers to try out the concepts in real-time.

Opinions

  • The author suggests that the appendChild() method is similar to the append() method but emphasizes the key differences, implying that understanding these differences is crucial for efficient DOM manipulation.
  • The article conveys that while appendChild() is limited to adding a single element at a time, it can be effectively used to add multiple elements by repeated method calls.
  • The author seems to favor the appendChild() method for adding nested elements due to its ability to return the appended element, which facilitates method chaining.
  • The use of Object.assign() in conjunction with appendChild() is presented as an elegant solution for creating and appending nested elements in a concise manner.
  • The inclusion of interactive CodePen examples indicates the author's belief in the importance of practical, hands-on learning experiences for understanding and mastering JavaScript concepts.

Add Element To Div Using appendChild() JavaScript

Learn how to add one or more HTML elements using appendChild()method in JavaScript.

1. Add Element Using appendChild()

Here is a simple HTML Markup

<section id="box-wrapper">
  <div class="box">div <br>1</div>
  <div class="box">div <br>2</div>
  <div class="box">div <br>3</div>
  <div class="box">div <br>4</div>
</section>

When you call the appendChild() method, it will add the newly created elements after the last child of the element inside the parent element.

Which is similar to the append() method.

But there are a few key differences between append() and appendChild() method.

const boxWrapper = document.getElementById("box-wrapper");

const box = document.createElement("div");
box.innerHTML = 'div <br> 5';
box.style.backgroundColor = "orange";
box.classList.add("box");

boxWrapper.appendChild(box);

Try it out

2. Add Multiple Child Elements As Siblings

Unlike the append() method, the appendChild() only allows you to add a single element in a single query.

If you want to add more child elements for the same parent, you’ll need to call it again with a new element as an argument.

const boxWrapper = document.getElementById("box-wrapper");

const box5 = document.createElement("div");
box5.innerHTML = 'div <br> 5';
box5.style.backgroundColor = "orange";
box5.classList.add("box");

const box6 = document.createElement("div");
box6.innerHTML = 'div <br> 6';
box6.style.backgroundColor = "orange";
box6.classList.add("box");

boxWrapper.appendChild(box5);
boxWrapper.appendChild(box6);

3. Add Nested Child Elements One Inside The Other

Using appendChild(), we can easily add one element inside another element by chaining the appendChild() methods.

Unlike the append() method, the appendChild() method returns the appended element.

As you can see, I’ve called the appendChild() method on the boxWrapper object.

The argument is Object.assign() which is one of the easiest ways to chain appendChild() to add nested child elements.

It takes two arguments.

  1. Element that you want to create, like div or buttons
  2. A JavaScript object where you’ll add one or more properties for the element.
boxWrapper.appendChild(
  Object.assign(
    document.createElement('div'),
    { classList : 'box',
      innerHTML: "div <br>5"
    }
  )
).appendChild(
  Object.assign(
    document.createElement("button"), {
      innerHTML: "Button"
    }
  )
)

In here, I first added the div elements which is box 5.

Then, I’ve chained the appendChild() method to add a child element inside the box 5 element which is the button.

Try it out

JavaScript
Programming
Javascript Tips
Javascript Development
Recommended from ReadMedium