Skip to content

Instantly share code, notes, and snippets.

@ceme
Created December 30, 2012 07:20
Show Gist options
  • Save ceme/4411403 to your computer and use it in GitHub Desktop.
Save ceme/4411403 to your computer and use it in GitHub Desktop.
document fragment DOM append
window.onload = function() {
//create DocumentFragment
var documentFragment = document.createDocumentFragment();
var list = document.getElementById( 'list' ); //<ul id="list"></ul>
var item = null;
//suppose this json is returned from ajax call
var ajaxResponse = [
{ 'name' : 'Haiku' },
{ 'name' : 'Linux' },
{ 'name' : 'OS X' },
{ 'name' : 'Windows' }
];
//add all names in ajaxReponse to documentFragment
for( var i in ajaxResponse ) {
item = document.createElement( 'li' );
item.appendChild( document.createTextNode( ajaxResponse[ i ].name ) );
documentFragment.appendChild( item );
}
//append all items in documentFragment to list
list.appendChild( documentFragment );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment