Use Array and for loop to add a list of text : createDocumentFragment « DOM Node « JavaScript Tutorial






<html>
<head>
<title>insertBefore() Example</title>
<script type="text/javascript">
    function addMessages() {
       var arrText = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"];

       var oFragment = document.createDocumentFragment();

       for (var i=0; i < arrText.length; i++) {
          var oP = document.createElement("p");
          var oText = document.createTextNode(arrText[i]);
          oP.appendChild(oText);
          oFragment.appendChild(oP);
       }

       document.body.appendChild(oFragment);

    }
</script>
</head>
<body onload="addMessages()">
</body>
</html>








23.4.createDocumentFragment
23.4.1.Use Array and for loop to add a list of text