Pekka
Pekka

Reputation: 449613

Determine a DOM element's document?

I am getting a reference to a DOM element from a WYSIWYG Editor.

I don't understand JS nor the WYSIWYG Editor (CKEditor) that deeply yet, but it somehow seems to be a pointer or other direct reference to the actual element that resides in an IFRAME within the WYSIWYG editor. At least, when I console.log the element, I get a link that, when clicked, opens the actual element in Firebug.

Is there a way to get a reference to this element's document object within the IFRAME?

Upvotes: 0

Views: 933

Answers (2)

Alex Martelli
Alex Martelli

Reputation: 882103

I don't know that specific editor, but if it has a reasonably normal implementation of the DOM, each node (including the DOM element to which you get a reference) has a parentNode read-only property that references its parent node. By following the chain of parentNode references, you're moving upwards in the DOM tree and should eventually reach the document you want.

(The ownerDocument property offers a more immediate solution, but it was not supported in some old browsers such as IE 5.5 -- if you don't have to worry about such "archaeology" issues, it's fine, but parentNode works even more broadly).

Upvotes: 1

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827694

If you have the DOM element reference, you can use the ownerDocument property:

var ownerDoc = someElement.ownerDocument;

Upvotes: 3

Related Questions