tags:

views:

48

answers:

2

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?

+2  A: 

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

var ownerDoc = someElement.ownerDocument;
CMS
+1 fast fingers man :)
gnarf
That worked, thank you.
Pekka
You're welcome @Pekka, it will work without problems on most browsers... http://www.quirksmode.org/dom/w3c_core.html
CMS
+1  A: 

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).

Alex Martelli
Cheers Alex, and very good additional information. I can assume IE > 6 so ownerDocument will do for me. I would upvote this but have run out of votes for today.
Pekka
@Pekka, NP, I'm maxed out for the day in any case;-). Yeah, mostly one doesn't have to worry about IE <6 any more (worrying about IE6 is bad enough in the general case;-), but not knowing that editor I thought its DOM might be based on an underlying ancient browser, so I offered the "rock-solid" solution just in case;-)
Alex Martelli