How to dynamically create new elements in JavaScript?

In this article, we will learn how to dynamically create new elements in JavaScript with the help of the createElement browser API.

Dynamic element creation in JavaScript allows you to generate new HTML elements on the fly. Whether you need to add content to a web page based on user interactions or dynamically generate elements for a specific task, it can help enhance the flexibility and interactivity of web applications.

Let's understand how to implement this with the help of some examples.

Using createElement() and appendChild()

In this example, we will have a button element and on clicking it, we will generate a new "p" element and append it to the DOM.

<!DOCTYPE html>
<html lang="en">
   <head>
      <title>How to dynamically create new elements in JavaScript?</title>
   </head>
   <body>
      <h3>How to dynamically create new elements in JavaScript?</h3>
      <div id="container">
         <!-- Newly created elements will be appended here -->
      </div>
      <button onclick="createNewElement()">Create Element</button>

      <script>
         function createNewElement() {
            // Create a new paragraph element
            var newParagraph = document.createElement('p');

            // Set the text content of the paragraph
            newParagraph.textContent = 'This is a dynamically created paragraph.';

            // Append the paragraph to the container div
            var container = document.getElementById('container');
            container.appendChild(newParagraph);
         }
      </script>
   </body>
</html>

Multiple Methods for Dynamic Element Creation

JavaScript provides several methods to create elements dynamically. Here's an example demonstrating four different approaches:

<!DOCTYPE html>
<html lang="en">
   <head>
      <title>Dynamic Element Creation Methods</title>
   </head>
   <body>
      <h3>Dynamic Element Creation Methods</h3>
      <div id="container">
         <!-- Newly created elements will be appended here -->
      </div>
      <div id="original">Original content</div>

      <button onclick="createWithInsertHTML()">insertAdjacentHTML</button>
      <button onclick="createWithClone()">cloneNode</button>
      <button onclick="createWithAppend()">appendChild</button>
      <button onclick="createWithWrite()">document.write</button>

      <script>
         function createWithInsertHTML() {
            var container = document.getElementById('container');
            container.insertAdjacentHTML('beforeend', 
               '<p>Created with insertAdjacentHTML method.</p>');
         }

         function createWithClone() {
            var originalElement = document.getElementById('original');
            var clonedElement = originalElement.cloneNode(true);
            var container = document.getElementById('container');
            container.appendChild(clonedElement);
         }

         function createWithAppend() {
            var newDiv = document.createElement('div');
            newDiv.textContent = 'Created with appendChild method.';
            var container = document.getElementById('container');
            container.appendChild(newDiv);
         }

         function createWithWrite() {
            document.write('<p>Created with document.write method.</p>');
         }
      </script>
   </body>
</html>

Comparison of Methods

Method Use Case Performance Recommendation
createElement() Creating new elements Fast Recommended
insertAdjacentHTML() Inserting HTML strings Fast Good for HTML content
cloneNode() Duplicating existing elements Fast For copying elements
document.write() Legacy method Slow Avoid in modern code

Key Points

  • createElement(): The most common and recommended method for creating new elements
  • appendChild(): Adds the created element to the DOM tree
  • textContent: Sets the text content safely without HTML parsing
  • insertAdjacentHTML(): Useful when you need to insert HTML strings directly

Conclusion

Dynamic element creation in JavaScript provides powerful capabilities for building interactive web applications. The createElement() and appendChild() combination is the most reliable approach for creating and inserting new elements into the DOM.

Updated on: 2026-03-15T23:19:01+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements