Questions tagged [documentfragment]

a DocumentFragment is a lightweight container that can hold DOM nodes

DocumentFragment is a "lightweight" or "minimal" Document object. It is very common to want to be able to extract a portion of a document's tree or to create a new fragment of a document. Imagine implementing a user command like cut or rearranging a document by moving fragments around. It is desirable to have an object which can hold such fragments and it is quite natural to use a Node for this purpose. While it is true that a Document object could fulfil this role, a Document object can potentially be a heavyweight object, depending on the underlying implementation. What is really needed for this is a very lightweight object. DocumentFragment is such an object.

Syntax

An empty DocumentFragment can be created using the below syntax.

var docFragment = document.createDocumentFragment();

where,

docFragment is a reference to an empty DocumentFragment object.

References

  1. www.w3.org - Interface DocumentFragment
  2. Document Fragment - MDN Link
98 questions
7
votes
3 answers

Initialisation of Custom Elements Inside Document Fragment

Consider this HTML template with two flat x-elements and one nested. How to…
7
votes
3 answers

Does jQuery use create document fragment inside each loops?

So I've read that jQuery uses document fragments internally to make rendering faster. But I am wondering if anyone knows if jQuery would use createDocumentFragment in this situation where I'm appending img elements to the DOM using the each…
Matt Whitehead
  • 1,743
  • 3
  • 19
  • 34
6
votes
3 answers

How to parse a DocumentFragment with with the Java standard DOM API

This is how I can parse a well-formed XML document in Java: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // text contains the XML content Document doc =…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
6
votes
3 answers

Cannot create DocumentFragment storing td, tr or th?

Is it possible to have DocumentFragments contain tr, th or td tags? If I do this: var template = document.createRange().createContextualFragment( '
' ); console.log(template.childNodes); I get the output of…
Raiden616
  • 1,545
  • 3
  • 18
  • 42
6
votes
1 answer

Why is DocumentFragment cleared after appending

The first log returns a full li element while the second one returns an empty DocumentFragment. Why? I couldn't find any information about that behavior in any documentation. var main = document.getElementById('main'); var fooTemplate =…
Lithy
  • 817
  • 12
  • 23
6
votes
1 answer

What are those "#document-fragment" elements within input elements in Chrome Inspector?

Since some time, when I inspect an input in Chrome, I see those strange #document-fragment greyed-out elements. I also notice that sometimes there are more, and sometimes there are less of them. What are these things? And is there a way to hide them…
Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152
5
votes
2 answers

Why does not DocumentFragment has getElementsByName?

I am following the instructions given on MDN to use