views:

19

answers:

1

Hi, please take a look at the following code.

  var oFra = document.createDocumentFragment();
    var myDiv = document.createElement("div");
    myDiv.id="myId";
    oFra.appendChild(myDiv);
    oFra.getElementById("myId");

In this case do i have ref to the div i just inserted inside documentFragement using the variable myDiv? Lets say i move ahead and add this documentFragement to the actual DOM. Will I still be able to access the div with id="myId" using this "myDiv" variable???

A: 

If you try this, it works: http://www.jsfiddle.net/dactivo/4BSaF/

The problem is that you cannot use "oFra" + getElementById directly, once you append the fragment, you can access the div "myId" in the DOM.

  <div id="test"></div>
<script type="text/javascript">
     var oFra = document.createDocumentFragment();
        var myDiv = document.createElement("div");
        myDiv.id="myId";
    myDiv.innerHTML="hola";
        oFra.appendChild(myDiv);
       // oFra.getElementById("myId");


    document.getElementById("test").appendChild(oFra);

    alert(document.getElementById("myId").innerHTML);

</script>
netadictos