-1

I'm trying to find an <h1> tag within an iframe with an id of f1.

Simply trying $("#f1").find("h1") returned an empty set.

So I thought maybe it was because the iframe didn't have jquery in it, so I couldn't use those methods.

So I tried a couple of different things. Without jquery:

var el = document.getElementById;
el.getElementsByTagName("h1");

no luck. Sort of doesn't really make sense either since getElementsByTagName is a method of the document. Though I sort of thought maybe an iframe would be a separate document.

Lastly I tried to add jquery

var el = document.getElementById("f1")
var s = document.createElement("script");
s.src = "http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js";
el.appendChild(s);

which gets jquery appended to the iframe. I can see it in the inspector in the head of the document embedded in the iframe. Yet

var el = document.getElementById("f1");
el.find("h1");

gets the typeError Object #<HTMLIFrameElement> has no method 'find

How can I get the value of my <h1> tag?

1252748
  • 14,597
  • 32
  • 109
  • 229

2 Answers2

1

Assuming the content of the iframe is from the same domain as the parent window, you can use contents():

$("#f1").contents().find("h1");

If the domains are different, you're out of luck as the Same Origin Policy will deny you access.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
-1

Try this

$("iframe#f1").contents().find("h1");
Alex
  • 1,336
  • 10
  • 19