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?
parentNode
in that code ?! – CarnyMxui.Layout.Positionable
in javascriptMVC. It's purpose is to move element from one place to another one:this.element[0].parentNode.removeChild(this.element[0]); document.body.appendChild(this.element[0]);
– GorgaappendChild
should be enough. An element can only exist at one place anyway, so it will be removed at the former place automatically. – Thiosinamine