Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery select element in parent window

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); 
like image 435
Rob Avatar asked May 16 '11 13:05

Rob


People also ask

How to parent element in jQuery?

The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.

Which jQuery method returns the direct parent element of the selected element?

jQuery parent() Method The parent() method returns the direct parent element of the selected element.

What does $( Div parent will select?

The :parent Selector page on jQuery says: Select all elements that have at least one child node (either an element or text). So $('div') would select all divs and $('div:parent') would select only those with children.

What is the difference between parent () and parents () methods in jQuery?

parent() method returns the direct parent element of the selected one. This method only traverse a single level up the DOM tree. parents() method allows us to search through the ancestors of these elements in the DOM tree.


1 Answers

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) 
like image 99
Dr.Molle Avatar answered Sep 28 '22 02:09

Dr.Molle