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

  • Home
  • SEARCH
  • 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 7616313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:51:31+00:00 2026-05-31T02:51:31+00:00

I have many elements that one of my JS widgets needs to create and

  • 0

I have many elements that one of my JS widgets needs to create and add to the DOM often. They never change.

So one option would be to store the HTML itself as a string in JS and use JQuery to create the elements from the string, then append it to the document:

var elements = "<div><table><tr><td>1</td><td>2</td></tr></table></div>";
function create() {
  return $(elements);
}
$("body").append(create());

Another option is to write a function that will use document.createElement(“div”), or $(“<div>”) many times to build the elements, append them to each other where needed, then append to the document:

function create() {
  return $("<div>").append($("<table>")......
}
$("body").append(create());

In the first case, I have a big JS string that is actually HTML. In the second case, I have an unwieldy piece of JS that actually represents HTML.

Are there (dis)advantages to one or the other? Is there a better solution I’m not thinking of?

  • 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-31T02:51:32+00:00Added an answer on May 31, 2026 at 2:51 am

    Note: If you hate reading, just check summary down below for final answer

    Maybe you don’t really need to create those with help of jQuery.

    If the structure of that html is complicated (hence using document.createElement approach would be an overkill) I would go with innerHTML attribute.

    // somewhere in your code, preferably outside of global scope
    var div = document.createElement('div')
    div.id = 'mycustomdiv'
    document.getElementsByTagName('body')[0].appendChild(div);
    // assuming elements contains string of html with your elements
    div.innerHTML = elements;
    

    That way you avoid the (assuming again) unnecessary overhead of creating and wrapping the elements in jQuery object.


    Update: test for yourself what’s the fastest method http://jsperf.com/creating-complex-elements. This test confirms that when you’re trying to squeeze every last bit of performance revert to vanilla javascript and classic DOM operations.


    Update 2. To investigate why innerHTML method on Firefox 10 have such bad results in relation to passing full string to jQuery.append, I took a look at the jQuery source.

    As it turns out (in jQuery 1.7.1), they’re using yet another way of creating dom elements by utilizing document.createDocumentFragment (with some fallbacks of course for browsers that don’t have proper support).

    DocumentFragments are DOM Nodes. They are never part of the main DOM tree. The usual use case is to create the document fragment, append elements to the document fragment and then append the document fragment to the DOM tree. In the DOM tree, the document fragment is replaced by all its children.

    Since the document fragment is in memory and not part of the main DOM tree, appending children to it does not cause page reflow.

    Assuming createDocumentFragment is available it turns out to be the best approach in terms of overall cross-browser performance of script.

    So, to sum up:

    I stand corrected. If you’re looking for best performance throughout different browsers when creating new DOM elements, focus on document fragments (use jQuery if you don’t want to deal with various corner cases yourself).

    For more reading concerning documentFragment check this post on John Resig blog http://ejohn.org/blog/dom-documentfragments/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a queue of Strings that will have many identical elements
I have a DIV with many elements that are all clickable. Each one with
I have a xml that has so many elements and most of that contain
I have a form with many form elements that are not allways populated with
I have a click function bound to many elements. It is possible that sometimes
I am coding some program that among many other operations have one that from
I have a generated table that has too many <td> elements. I call $('#Container
I have a webpage that has many user-generated elements that need to be cleaned
I have many elements that have texts and I want to centralize it all
how can i do. i have many html elements on page. after clicking on

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.