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 8940803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:08:20+00:00 2026-06-15T11:08:20+00:00

If I do this: document.getElementById(myDiv).innerHTML = some_html_code; will that create nodes in my DOM

  • 0

If I do this:

document.getElementById("myDiv").innerHTML = "some_html_code";

will that create nodes in my DOM three as it would if I used appendChild()?

Reason for asking is that I’m creating a mobile application where memory usage must be low. I don’t want to create a lot of nodes.

  • 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-06-15T11:08:21+00:00Added an answer on June 15, 2026 at 11:08 am

    It is roughly the same as

    var div = document.getElementById("myDiv");
    
    while( div.firstChild ) {
        div.removeChild( div.firstChild );
    }
    
    div.appendChild( document.createTextNode("a_html_string") );
    

    Of course, if by "html_string" you mean a string consisting of complex html, then of course nodes are created from the html as appropriate (element nodes, comment nodes, text nodes etc). But a simple string of text is simply a single text node.

    So if your html string were '<div id="hello">world</div>', that would roughly be:

    var div = document.getElementById("myDiv");
    
    while( div.firstChild ) {
        div.removeChild( div.firstChild );
    }
    
    var anotherDiv = document.createElement("div");
    anotherDiv.setAttribute("id", "hello");
    anotherDiv.appendChild(document.createTextNode("world"));
    div.appendChild(anotherDiv);
    

    It is probably shocking how much is happening with a simple innocent looking .innerHTML setter, and this is not even including parsing the html.

    It’s important to note that none of this is garbage, all of those objects created are necessary. To make sure you are only creating necessary nodes, do not use unnecessary whitespace between nodes. For example

    <span>hello</span> <span>world</span>
    

    is 3 text nodes but

    <span>hello</span><span> world</span>
    

    is only 2 text nodes.

    A long while ago I created a facetious jsfiddle that converts html to “DOM code” for all of those .innerHTML haters.

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

Sidebar

Related Questions

I have some code like this: var mySelect = document.getElementById(mySelect); mySelect.innerHTML = <option>Loading...</option>; $.get(ajaxPage.php,
my code: for(var myLine = 0; myLine < 100; myLine++) document.getElementById(myDiv).innerHTML += line +
I use this script : <script language=javascript> function toggle() { var ele = document.getElementById(mydiv);
I have this js code: var id = document.getElementById(myDiv).getAttribute(data-id); et=Your Message Here; el=http://x2.xclicks.net/sc/out.php?s=+id+ sl=new
Wondering why I can't get document.getElementById("my_div").innerHTML to update the DOM when I re-assign the
Is there a reason why I can do this: document.getElementById('gridID').style.background='blue'; but when I try
Why this works document.getElementById(myCheckbox).checked = false; whereas this doesn't with jquery: $(myCheckbox).attr(checked) = false;
Maddening problem here. When my page loads: <body onload=getClientDateTime();> It runs this function: document.getElementById('ClientDateTime').value=hello
This works : alert(document.getElementById(Container).nodeName); But this doesnt : var CurParent = document.getElementById(Container); alert(CurParent.nodeName); I
I currently have this: function submit() { document.getElementById(lostpasswordform).click(); // Simulates button click document.lostpasswordform.submit(); //

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.