I wanted to know if a single document fragment can be used to insert several fragment in the DOM, or do I have to create new ones for each element I want to insert.
Can I do the example below:
var frag = document.createDocumentFragment(),
div = document.createElement('div'),
section = document.createElement('section'),
header = document.createElement('header'),
divFrag = frag.appendChild(div),
sectionFrag = frag.appendChild(section),
headFrag = frag.appendChild(header);
Many thanks
Just a note about your code:
appendChildreturns the DOM element inserted, so:will return
div. i.e.div === divFrag.To answer your question, you can re-use a document fragment.
If, for example, you wanted to append all elements to the
<body>, you could do it like so: