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.
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.
EDIT:
If you want to manipulate the completed fragment using jQuery before you append it, you need to do:
as this will not work properly: