Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 823641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:58:46+00:00 2026-05-15T02:58:46+00:00

In practice, what are the advantages of using createElement over innerHTML? I am asking

  • 0

In practice, what are the advantages of using createElement over innerHTML? I am asking because I’m convinced that using innerHTML is more efficient in terms of performance and code readability/maintainability but my teammates have settled on using createElement as the coding approach. I just wanna understand how createElement can be more efficient.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:58:47+00:00Added an answer on May 15, 2026 at 2:58 am

    There are several advantages to using createElement instead of modifying innerHTML (as opposed to just throwing away what’s already there and replacing it) besides safety, like Pekka already mentioned:

    Preserves existing references to DOM elements when appending elements

    When you append to (or otherwise modify) innerHTML, all the DOM nodes inside that element have to be re-parsed and recreated. If you saved any references to nodes, they will be essentially useless, because they aren’t the ones that show up anymore.

    Preserves event handlers attached to any DOM elements

    This is really just a special case (although common) of the last one. Setting innerHTML will not automatically reattach event handlers to the new elements it creates, so you would have to keep track of them yourself and add them manually. Event delegation can eliminate this problem in some cases.

    Could be simpler/faster in some cases

    If you are doing lots of additions, you definitely don’t want to keep resetting innerHTML because, although faster for simple changes, repeatedly re-parsing and creating elements would be slower. The way to get around that is to build up the HTML in a string and set innerHTML once when you are done. Depending on the situation, the string manipulation could be slower than just creating elements and appending them.

    Additionally, the string manipulation code may be more complicated (especially if you want it to be safe).

    Here’s a function I use sometimes that make it more convenient to use createElement.

    function isArray(a) {
        return Object.prototype.toString.call(a) === "[object Array]";
    }
    
    function make(desc) {
        if (!isArray(desc)) {
            return make.call(this, Array.prototype.slice.call(arguments));
        }
    
        var name = desc[0];
        var attributes = desc[1];
    
        var el = document.createElement(name);
    
        var start = 1;
        if (typeof attributes === "object" && attributes !== null && !isArray(attributes)) {
            for (var attr in attributes) {
                el[attr] = attributes[attr];
            }
            start = 2;
        }
    
        for (var i = start; i < desc.length; i++) {
            if (isArray(desc[i])) {
                el.appendChild(make(desc[i]));
            }
            else {
                el.appendChild(document.createTextNode(desc[i]));
            }
        }
    
        return el;
    }
    

    If you call it like this:

    make(["p", "Here is a ", ["a", { href:"http://www.google.com/" }, "link"], "."]);
    

    you get the equivalent of this HTML:

    <p>Here is a <a href="http://www.google.com/">link</a>.</p>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Folks, I'm using the best-practice of prepared sql statements to execute many Inserts/Updates that
Possible Duplicate: Initializing in constructors, best practice? Advantages of using initializer list? I have
I've a best practice question on CouchDB (actually I'm using TouchDB a CouchDB port
I'm using a Java class library that is in many ways incomplete: there are
Are there any advantages or benefits to using tertiary operators for if statements? For
Is there a best practice for using email/user accounts for 3rd part APIs in
I have a structure that can be very easily represented using a three-deep nested
Possible Duplicate: Advantages of using prototype, vs defining methods straight in the constructor? What
Possible Duplicate: Advantages and Disadvantages of using System.out.printf(); In a discussion in an online
Is creating user defined types instead of using existing types best practice? In my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.