65

Let's assume I have a jQuery object of a DIV element in body. I'd like to obtain document object by traversing. Can it be possible ?

Note: window.document is not a option in my case.

Thank you.

Ozgur
  • 1,744
  • 4
  • 17
  • 24
  • I guess so, walking from parent to parent... But I *think* every DOM element has a reference to its document anyway somewhere, let me check – Pekka Nov 11 '10 at 19:10

3 Answers3

127

element.ownerDocument will give you a reference to the document to which any DOM element belongs.

casablanca
  • 69,683
  • 7
  • 133
  • 150
1

Yes, the document object is the parent of the <HTML> element (at least in Firefox). Find it like this:

function FindDoc(el) {
    while(el.parentNode) {
        el = el.parentNode;
    }
    return el;
}
palswim
  • 11,856
  • 6
  • 53
  • 77
  • That would be slower than directly accessing it. Why not try http://stackoverflow.com/questions/1338224/how-do-i-get-the-window-object-from-the-document-object – NoBugs Aug 22 '13 at 07:31
0

firstElementChild The first child that is an element node Traversal module.

Andrew Collins
  • 2,541
  • 3
  • 17
  • 16