Samul
Samul

Reputation: 2019

How to get the current document of an element with jQuery

I have a complex code that travels inside windows and iframes (yes, windows cause I open some windows with window.open sometimes and also travel inside iframes) and when some condition apply I get an element from inside of those iframes (they usually are DIVs and SPANs).

So, I have the element that I want in the object "$(this)" so from the parent window how can I know the "document" element that has this element? I need to get the "document" element that has "$(this)" and set some attributes to it.

I tried $(this).parents(document) but it does not work.

Upvotes: 0

Views: 2190

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074445

If this refers to an element (such that $(this) would give you a jQuery wrapper around it) or indeed any Node, then this.ownerDocument is a reference to the document the element is in (null if it's not in a document). Details in ownerDocument in the specification.

Upvotes: 3

Related Questions