Created
December 30, 2012 07:20
-
-
Save ceme/4411403 to your computer and use it in GitHub Desktop.
document fragment DOM append
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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