jQuery and iFrames

When you get a jQuery object for an iframe the html() and other methods might not function as you would expect them. That is because what you really want is the jQuery object for the ‘document’ of the iframe. How you get the document object depends on the browser.

To simplify things I created a jQuery plugin that allows you to call iframe() instead of jQuery’s $() or find() and returns a jQuery object that should behave the way you expect.

So instead of this which wouldn’t work

$("#MyIframe").html("new HTML");

You would do this:

$.iframe("#MyIframe").html("new HTML");

And instead of this which wouldn’t work:

$("div").find("#MyIframe").html("new HTML");

You would do this:

$("div").iframe("#MyIframe").html("new HTML");

You can download it here: jquery.iframe.js

Just include it after you include jQuery.

Leave a Reply

Your email address will not be published. Required fields are marked *