Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery just created empty div doesn't have document fragment as parent

When creating new html node in jQuery using

$('<some-node>some html code</some-node>');

it won't become part of DOM, until you attach it. However, it does not mean, the node has no parent.

If the node was created unempty, e.g.:

var myNewNode = $('<div>Hello</div>');

You can check the parent:

myNewNode[0].parentNode; // Who is the parent?

and see you get

DocumentFragment

as result. DocumentFragment is some object similar to document, however, not part of the DOM tree.

The strange thing comes now. When you create an empty node, like

var myNewEmptyNode = $('<div></div>');

and try to check its contents

myNewEmptyNode[0].parentNode; // Who is now the parent?

surprisingly you get

null

I cannot understand this behaviour and found nothing about it in jQuery documentation. I found it when trying to debug why javascriptMVC mxui modal was failing on an empty div.

I have tested this behaviour in both Chromium and Opera, so it does not seem to be a browser related issue.

Does someone have an explanation for this?

like image 574
Samuel Hapak Avatar asked Apr 29 '12 12:04

Samuel Hapak


People also ask

How do you use DocumentFragment?

A common use for DocumentFragment is to create one, assemble a DOM subtree within it, then append or insert the fragment into the DOM using Node interface methods such as appendChild() , append() , or insertBefore() . Doing this moves the fragment's nodes into the DOM, leaving behind an empty DocumentFragment .

What is createContextualFragment?

createContextualFragment() method returns a DocumentFragment by invoking the HTML fragment parsing algorithm or the XML fragment parsing algorithm with the start of the range (the parent of the selected node) as the context node.

What is create document fragment?

Usage notes DocumentFragment s are DOM Node objects which are never part of the main DOM tree. The usual use case is to create the document fragment, append elements to the document fragment and then append the document fragment to the DOM tree. In the DOM tree, the document fragment is replaced by all its children.

What is. appendChild?

appendChild() The appendChild() method of the Node interface adds a node to the end of the list of children of a specified parent node. If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position.


2 Answers

That's due to the fact that jQuery uses document.createElement for "quick" HTML strings, but jQuery.buildFragment for all other (more "complex") HTML strings. <div></div> is considered quick, whereas <div>a</div> is not.

I set up a fiddle so you can check: http://jsfiddle.net/PzBSY/2/.

"Quick" is defined with the regular expression:

var rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/;

which passes the empty div string, but not the non-empty div string. Note the if/else block after the check which makes for the two branches.

So it's basically because jQuery explicitly builds a document fragment with the non-empty div, whereas it does not with the empty div (it just uses document.createElement("div") instead).

like image 173
pimvdb Avatar answered Oct 14 '22 07:10

pimvdb


Here target may be any valid jQuery selectors.

only $('<div></div>') does not create any node to your DOM. It just create it virtually but not placed it to Document. To inset it into your DOM you need to use method like append(), appendTo(), insert() etc.

$('target').append($('<div class="latest"></div>'));
$('div.latest').parent();

Here I inset a div with class latest and after appending it retrieve its parent node using parent() method.

like image 26
thecodeparadox Avatar answered Oct 14 '22 08:10

thecodeparadox