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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:27:58+00:00 2026-06-14T13:27:58+00:00

This post was the most helpfull to understand createDocumentFragment() instead of createElement() Should I

  • 0

This post was the most helpfull to understand createDocumentFragment() instead of createElement()
Should I use document.createDocumentFragment or document.createElement

I’ve understood that for performance reason using fragment will help on big dataset so i want to conver my function.

This is what i use right now and it works as desired => Get content from a php file with ajax and then append this content at the top of existing div#wrapperinside a new div.feedBox(r being the XMLHTTP /ACTIVE OBJECT)

r.onreadystatechange=function(){
    if(r.readyState==4 && r.status==200){

        //Want to convert this to createDocumentFrangment --START
        var n = document.createElement("div");
        n.className = "feedBox";
        n.innerHTML = r.responseText;
        document.getElementById("wrapper").insertBefore(n, document.getElementById("wrapper").firstChild);
        //Want to convert this to createDocumentFrangment --END
    }
}

This is what i tried, but what happens is the content is added but without the div.feedBox

var n = document.createElement("div");
n.className = "feedBox";
n.innerHTML = r.responseText;
var f = document.createDocumentFragment();
while (n.firstChild) { f.appendChild(n.firstChild); }
document.getElementById("wrapper").insertBefore(f, document.getElementById("wrapper").firstChild);

What did i miss? can you explain why and how to make it work?
Is this really a more efficient way of doing this?

PS: NO jquery please. I know it well and i use it widely on other project but i want this to be as small / lite / efficient as possible.

  • 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-14T13:28:00+00:00Added an answer on June 14, 2026 at 1:28 pm

    Shouldn’t this line

    while (n.firstChild) { f.appendChild(n.firstChild); 
    

    be

    f.appendChild(n);
    

    Also I see that you are not appending the div.feedBox to your DOM anywhere..

    What happens if the while condition fails.. You are not appending anything to your DOM..

    I am assuming this will work .. Not tested though

    f.appendChild(n)
    document.getElementById("wrapper").appendChild(f,        
                                     document.getElementById("wrapper").firstChild);
    

    ALso better to use

    .appendChild(f, instead of .insertBefore(f,

    Check Fiddle

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

Sidebar

Related Questions

This post on SO answers most of the questions I have (much thanks to
This post identifies a feature that I would like to disable. Current numpy behavior:
I follow this post What is the simplest and most robust way to get
edit Re-writing most of this post as I've made some improvements to the code
This question is a follow up to this post: What is the most efficient/elegant
Most questions revolving around the title of this post ask about making Hibernate, or
from this post What is the most efficient way to store tags in a
I am using this post : What is the simplest and most robust way
This post shows how to axiomatise a length function for Z3's built-in lists. However,
This post asks this question but doesn't really give an answer, so I thought

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.