Setting Focus to an iFrame in IE

extjs, focus, iframe, javascript, jquery

Solution

You need to call the `focus()` method of the iframe's `window` object, not the iframe element. I'm no expert in either jQuery or ExtJS, so my example below uses neither.

function focusIframe(iframeEl) {
    if (iframeEl.contentWindow) {
        iframeEl.contentWindow.focus();
    } else if (iframeEl.contentDocument && iframeEl.contentDocument.documentElement) {
        // For old versions of Safari
        iframeEl.contentDocument.documentElement.focus();
    }
}

Problem

There is a tree menu in my application and on click of the menu items, it loads a url in a iFrame. I like to set the focus in an element of the page loaded in the iFrame. I'm using this code, and it works perfectly in all the browsers except IE: ``` var myIFrame = $("#iframeName"); myIFrame.focus(); myIFrame.contents().find('#inputName').focus(); ``` I have tried all different options like using setTimeout, but no chance. After the page loads, when I hit the tab key, it goes to the second input, which means it's been on the first input, but it doesn't show the cursor! I am using ExtJS and the ManagedIFrame plugin. Any help is appreciated.

Original source