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

The Archive Base Latest Questions

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

I swear this was just working fine a few days ago… elm = document.querySelectorAll(selector);

  • 0

I swear this was just working fine a few days ago…

elm = document.querySelectorAll(selector);
var frag = document.createDocumentFragment();
while (elm[0]){
    frag.appendChild(elm[0]);
}

Right, so, this should append each node from our elm node list. When the first one is appended, the second “moves” to the first position in node list, hence the next one is always elm[0]. It should stop when the elm nodeList is completely appended. However, this is giving me an infinite loop. Thoughts?

EDIT – because I’ve gotten the same answer several times…
A nodeList is not an array, it is a live reference. When a node is “moved” (here, appended) it should be removed automatically from the node list. The answers all saying “you’re appending the same element over and over” – this is what’s happening, it shouldn’t be. A for loop shouldn’t work, because when the first node is appended, the next node takes its index.

2nd EDIT

So the question is now “why is the nodeList behaving as an array?”. The node list SHOULD be updating every time a node is being appended somewhere. Most peculiar.

Solution (in case someone needs something to handle live + non-live node lists)

elm = (/*however you're getting a node list*/);
var frag = document.createDocumentFragment();
var elength = elm.length;
for (var b = 0; b<elength; b++){
    if (elm.length === elength){
        frag.appendChild(elm[b]);
    } else {
        frag.appendChild(elm[0].cloneNode());
    }
}

Basically, just checking to see if the node list has changed length.

  • 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-14T04:56:28+00:00Added an answer on June 14, 2026 at 4:56 am

    From the MDN Docs

    Element.querySelectorAll

    Summary

    Returns a non-live NodeList of all elements descended from the element on which it is invoked that match the specified group of CSS selectors.

    Syntax

    elementList = baseElement.querySelectorAll(selectors);
    

    where

    • elementList is a non-live list of element objects.
    • baseElement is an element object.
    • selectors is a group of selectors to match on.

    From the docs above you can see it does not automatically remove it when you append it to another element since it is non live. Run a demo to show that feature.

    var selector = "div";
    elm = document.querySelectorAll(selector);
    var frag = document.createDocumentFragment();
    console.log("before",elm.length);
    frag.appendChild(elm[0]);
    console.log("after",elm.length);
    

    When the code above runs, in the console you get.

    before    3
    after     3
    

    If you want to do the while loop, convert to an array and shift() the items off

    var selector = "div";
    var elmNodeLIst = document.querySelectorAll(selector);
    var frag = document.createDocumentFragment();
    var elems = Array.prototype.slice.call(elmNodeLIst );
    while (elems.length) {
        frag.appendChild(elems.shift());
    }
    console.log(frag);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I could swear this was working the other day: var resultSet = (from o
I swear this script worked fine last night but can't get it to work
I swear, I have this exact thing working on another page. I'm such a
I swear this was working last night which was why I was able to
I could swear that just a couple of months ago I downloaded a copy
Some days I swear I'm going mad. This is one of those days. I
I swear I've done this before and it's worked fine, but it seems that
I swear this used to work, but it's not in this case. I'm trying
I swear this assignment will be the end of me, I've been researching this
I've done my homework - I swear, I've been researching this for a bit.

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.