get an element from outside of iframe jquery

Get an Element from Outside of iFrame jQuery

If you have an iFrame in your HTML document and you want to access the elements outside of it using jQuery, you can do so by using the parent() method. This method is used to access the parent document of an iFrame. Here's how:


var element = parent.$('#element-id');

In the above code, $ is used to access jQuery and '#element-id' is the ID of the element that you want to access.

If you want to access an element that is higher up the DOM tree, you can chain the parent() method. For example:


var element = parent.parent().$('#element-id');

In the above code, parent() is used twice to access the grandparent document of the iFrame.

If you have multiple iFrames on your page and you want to access an element outside of a specific iFrame, you can use the siblings() method to target the element you want. For example:


var element = parent.$('#iframe-id').siblings('#element-id');

In the above code, '#iframe-id' is the ID of the iFrame and '#element-id' is the ID of the element that you want to access.

Another way to access elements outside of an iFrame is by using the window.parent property. This property returns a reference to the parent window object. Here's how:


var element = window.parent.$('#element-id');

In the above code, window.parent is used to access the parent window object and $ is used to access jQuery.

By using one of these methods, you can easily access elements outside of an iFrame using jQuery.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe