You are here: Reference > JavaScript > client-side > HTML DOM > properties > ownerDocument

ownerDocument property

Browser support:
Returns the document object that contains the current node.
Elements and attributes always have an owner document regardless of they are inserted into (appendChild, insertBefore, setAttributeNode) or removed form (removeChild, removeAttributeNode) the document tree. When a node is created (createElement, createAttribute, createComment, etc.) its ownerDocument property is set to the document object on which the create method was called. Insert and remove operations do not affect the owner document of a node. If you need to move a node to another document, use the adoptNode and importNode methods.

Syntax:

object.ownerDocument;
You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

Reference to the container document object.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the ownerDocument property:
<head>
    <script type="text/javascript">
        function InsertButton () {
            var divObj = document.getElementById ("myDiv");
            var docObj = divObj.ownerDocument;

                // create a new element in the same document where the div is located
            var buttonObj = docObj.createElement ("button");
            buttonObj.innerHTML = "New Button";
                // insert the new element
            divObj.appendChild (buttonObj);
        }
    </script>
</head>
<body>
    <div id="myDiv" style="background:yellow;">
        Some content.
    </div>
    <br />
    <button onclick="InsertButton ();">Insert new button!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content