Use Array and for loop to add a list of text : createDocumentFragment : DOM Node JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » DOM Node » createDocumentFragment »

 

Use Array and for loop to add a list of text

















<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>







HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo DOM Node
» createDocumentFragment