jQuery select element in parent window

Rob picture Rob · May 16, 2011 · Viewed 112.5k times · Source

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

Answer

Dr.Molle picture Dr.Molle · May 16, 2011

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)