Tony_Henrich
Tony_Henrich

Reputation: 44095

How to test if an element resides in an iFrame using jQuery?

How to test if an element resides in an iFrame using jQuery and if it does, how to select that iFrame?

Upvotes: 3

Views: 2317

Answers (2)

alex
alex

Reputation: 490283

This will do it.

var elementDocument = $("#your-element").prop("ownerDocument");

var iframeOwner = $("iframe").filter(function() {
                      return this.contentDocument == elementDocument;
                  });

It ought to be quicker than the accepted answer.

Upvotes: 5

ShankarSangoli
ShankarSangoli

Reputation: 69915

Try this it will work for you.

var requiredIframe;
$("iframe").each(function(){
  if($(this).contents().find("elementYouAreLookingFor").length){
     requiredIframe = $(this);
     return false;
  }
});

Upvotes: 3

Related Questions