Set focus to an element in an iframe

If you’re trying to set focus to an element in an iframe that has already been loaded, you need to set focus to the iframe first, then to the element.

Here is sample code:

if (ie) {
var myIframe = document.frames['my_iframe'];
var myField = document.frames['my_iframe'].document.getElementById('my_field');
else {
var myIframe = document.getElementById('my_iframe');
var myField = document.getElementById('my_iframe').contentDocument.getElementById('my_field');
if (!safari) {

In my use case, Safari didn’t take the iframe focus properly, but I didn’t take the time to test a generic case. If someone has time or interest in testing, please add a comment with your findings.

Hopefully this prevents someone from wasting as much time on this as I did.