Working demo
Here you are dealing with moving JavaScript variables. Remove this line var elems = null;and your code should work.
It is considered best practice in JavaScript to declare all variables at the top of a function body.
Read this article for more information on JavaScript hoisting .
, , - . createDocumentFragment , , DOM. .
var elems = 10;
function generateElems() {
var d=document.createDocumentFragment();
for (var i = 0; i < elems; i++) {
d.appendChild(document.createElement('input'));
}
document.getElementsByTagName('div')[0].appendChild(d);
}
generateElems();