Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get iframe contents with a jquery selector

Is there anyway to access an iframe's contents via a selector? Something like this:

$("iframe::contents .my-foo")

I'm constantly accessing an iframe's contents for a project I'm currently working on and $("iframe").contents().find(".my-foo") is becoming a bit tedious to type out.

If this feature doesn't exist in jquery out of the box, is there a plugin that provides this functionality? If not how could I write such a plugin?

like image 794
Xavi Avatar asked May 13 '11 13:05

Xavi


1 Answers

I had this issue once where I found it tedious. I never found a solution for how to write a single selector like that.

Even so, the selector is still rather long. The most obvious solution to me is just save it to a variable.

var frame = $("iframe").contents();

frame.find(".my-foo")
...

Is that better?

like image 79
Tesserex Avatar answered Sep 28 '22 04:09

Tesserex