Skip to content
the poor coder

find inside iframe jquery

Poor Man
Published
Reading time
2 min read
On this page

How to find inside iframe using jQuery#

jQuery is a popular JavaScript library used for manipulating HTML documents, including iframes. If you need to find elements inside an iframe using jQuery, here are some ways to do it:

Method 1: Using the contents() method#

The contents() method is used to retrieve the document object inside an iframe. Here's an example:

Text
$(document).ready(function(){
  var iframeContents = $("#myiframe").contents();
  var innerDiv = iframeContents.find("div");
  console.log(innerDiv);
});

In the code above, we first use the contents() method to retrieve the contents of the iframe with an ID of "myiframe". We then use the find() method to find a div element inside the iframe. Finally, we log the innerDiv variable to the console.

Method 2: Using the contents() and filter() methods#

The filter() method is used to reduce the set of matched elements to those that match a certain selector. Here's an example:

Text
$(document).ready(function(){
  var iframeContents = $("#myiframe").contents();
  var innerDiv = iframeContents.filter("div");
  console.log(innerDiv);
});

In the code above, we first use the contents() method to retrieve the contents of the iframe with an ID of "myiframe". We then use the filter() method to find a div element inside the iframe. Finally, we log the innerDiv variable to the console.

Method 3: Using the contents() and find() methods with selectors#

You can also use selectors inside the find() method to find elements that match a certain criteria. Here's an example:

Text
$(document).ready(function(){
  var iframeContents = $("#myiframe").contents();
  var innerDiv = iframeContents.find("div.myclass");
  console.log(innerDiv);
});

In the code above, we first use the contents() method to retrieve the contents of the iframe with an ID of "myiframe". We then use the find() method with a selector to find a div element with a class of "myclass" inside the iframe. Finally, we log the innerDiv variable to the console.

Using any of these methods, you can easily find elements inside an iframe using jQuery.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC