jQuery select element in parent window
Solution 1
Use the context-parameter
$("#testdiv",parent.document)
But if you really use a popup, you need to access opener instead of parent
$("#testdiv",opener.document)
Solution 2
I looked for a solution to this problem, and came across the present page. I implemented the above solution:
$("#testdiv",opener.document) //doesn't work
But it doesn't work. Maybe it did work in previous jQuery versions, but it doesn't seem to work now.
I found this working solution on another stackoverflow page: how to access parent window object using jquery?
From which I got this working solution:
window.opener.$("#testdiv") //This works.
Solution 3
I came across the same problem but, as stated above, the accepted solution did not work for me.
If you're inside a frame or iframe element, an alternative solution is to use
window.parent.$('#testdiv');
Here's a quick explanation of the differences between window.opener, window.parent and window.top:
- window.opener refers to the window that called window.open( ... ) to open the window from which it's called
- window.parent refers to the parent of a window in a frame or iframe element
Solution 4
You can also use,
parent.jQuery("#testdiv").attr("style", content from form);
Rob
Updated on July 20, 2022Comments
-
Rob almost 2 years
Is there a way to select a DIV in the parent window using jQuery?
For example:
Main page contains this,
<div id="testdiv"></div>
Popup page has a form with some options and an 'Apply' button. When the user clicks apply it affects the style attribute on the main page.
Something along the logic of,
parent.$("#testdiv").attr("style", content from form);
-
Anil Sharma over 10 yearsAwesome mate ! @Dr.Molle was overconfident !
-
Manu over 7 yearsLearned new thing today, Thanks :)
-
Dinerdo almost 7 years..or it actually worked in 2011. For this answer's solution to work, would that popup need to already have jQuery loaded?
-
Akki619 almost 6 yearswhat if the iframe is in another domain? How to close it?