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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:15:23+00:00 2026-05-14T22:15:23+00:00

I use the following code to append a big dom on a mobile browser

  • 0

I use the following code to append a big dom on a mobile browser (webkit):

1. while(i--)  // 'i' ranges from 10 to possibly 1000
2. {
3.   var html01 = ['<div class="test">', someVal[i],'</div>',
4.                 '<div><p>', someTxt.txt1, anotherVal.val[i], '</p></div>',
5.   // lots of html snippets interspersed with variables that differ in each loop iteration
6.                  // on average ~40 to 50 elements in this array
7.                ].join('');
8.   var fragment = document.createDocumentFragment(),
9.   div = fragment.appendChild(document.createElement('div'));
10.  div.appendChild(jQuery(html01)[0]);
11.  someArray[someArray.length] = fragment;
12. } //end while loop
13. jQuery('#screen1').append(someArray);
14. // similarly i create 'html02' till 'html15' to append in other screen divs

Is there a better or faster way to do the above? Do you see any problems with the code? I am a little worried about line 10 where i wrap in jquery and then take it out.

  • 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-14T22:15:24+00:00Added an answer on May 14, 2026 at 10:15 pm

    UPDATE: Addresses the need to append unique values


    I’m no DOM API expert, but this will create the elements you need, clone them in the loop, update their textNode values, append them to the fragment, then append the fragment to the DOM.

    It will work as long as the variables you offered for the text nodes are correct.

        // Form a single fragment outside the loop
           var fragment = document.createDocumentFragment();
    
        // Create the first div and append a textNode
            var div1 = document.createElement('div');
            div1.appendChild( document.createTextNode('content') );
            div1.setAttribute('class','test');
    
        // Create the second div, its inner p element, and its textNode
            var div2 = document.createElement('div');
            var p = document.createElement('p');
            p.appendChild( document.createTextNode('content') );
            div2.appendChild( p );
    
        // Variables to store the clones of above
            var clone1, clone2;
    
        // Counter for while loop
            var i = 1000;
    
         while(i--)  // someIndex ranges from 10 to possibly 1000
         {
            // Clone the elements we created
            clone1 = div1.cloneNode(true);
            clone2 = div2.cloneNode(true);
    
            // Update the nodeValue of the first textNode in div1
            clone1.firstChild.nodeValue = 'someVal[i]';
    
            // Get the p element in div2 and update its nodeValue
            clone2.firstChild.firstChild.nodeValue = 'someTxt.txt1 + anotherVal.val[i]';
    
            // Append the elements we created, cloned and updated to the fragment
            fragment.appendChild(clone1).
            fragment.appendChild(clone2);
         }
    
            // Append the populated fragment to #screen1
         document.getElementById('screen1').appendChild(fragment);
    

    EDIT:

    If you want to manipulate the completed fragment using jQuery before you append it, you need to do:

    $(fragment.childNodes);  // Create a jQuery object of the content of the fragment
    

    as this will not work properly:

    $(fragment);   // Doesn't work. jQuery methods will be ineffective.    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.