Reputation: 663
What's the best possible way to find a given "div.myClass" element in the current document, as well as any iframes it (and their nested iframes, too) might have?
Thanks
Upvotes: 2
Views: 3643
Reputation: 349042
Provided that frame access is not restricted by the same origin policy:
function getElem(selector, $root, $collection) {
if (!$root) $root = $(document);
if (!$collection) $collection = $();
// Select all elements matching the selector under the root
$collection = $collection.add($root.find(selector));
// Loop through all frames
$root.find('iframe,frame').each(function() {
// Recursively call the function, setting "$root" to the frame's document
getElem(selector, $(this).contents(), $collection);
});
return $collection;
}
// Example:
var $allImageElements = getElem('img');
Upvotes: 6